开发者

jQuery Ajax call not working

开发者 https://www.devze.com 2023-01-11 08:49 出处:网络
i am just trying to make a simple Ajax call using jQuery this is my JavaScript: //Starts the game function startGame() {

i am just trying to make a simple Ajax call using jQuery

this is my JavaScript:

//Starts the game
function startGame() {                       
    $.ajax({
        type: "POST",
        url: "Default.aspx/StartGame"                
    });
}

my button:

<input type="image" value="twist..." src="images/play.png" class="playButton" onclick="startGame();return false;" />

and code 开发者_JS百科behind:

public partial class Default : Page
    {
        private static GameEngine GameEngine
        {
            get { return new GameEngine();}
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public void StartGame()
        {
             GameEngine.StartToPlay();   
        }
    }

when I debug the code in Visual Studio the method StartGame is never called.

Can anyone explain to me what's the problem?


I'm not familiar with .net, but it seems as if your Javascript code makes a POST request, while there's only a GET handler on the backend.

In general it's a good idea to be able to debug the functionality on each layer. For example, you can check if your request was triggered (or analyze what the problem was with the Firebug extension on Firefox. Chrome and Safari have similar debugging mechanisms.


I believe that because you are setting the request as POST and you aren't sending any POST data eg:

 $.ajax({
   type: "POST",
   url: "Default.aspx/StartGame",
   data: "name=John",
   success: function(msg){
     alert(msg);
   }
 });

If you are just retrieving html you should use:

$.ajax({
  url: "Default.aspx/StartGame",
  cache: false,
  success: function(html){
    //create the game html
    $("#game").append(html);
  }
});


From : http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

You're missing parameters (contentType & dataType) to $.ajax :

 function startGame() {
     $.ajax({
         type: "POST",
         url:"Default.aspx/StartGame" 
         contentType: "application/json; charset=utf-8",
         data: "{}",
         dataType: "json"
     });
 }


i made StartGame() static and now it works

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号