views:

71

answers:

4

Hi 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 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?

A: 

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.

poezn
A: 

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"
     });
 }
mathieu
Those aren't required parameters.
Matt Huggins
ok corrected + added source.Even with optional parameter, this solution works...
mathieu
A: 

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);
  }
});
Josh Stuart
A: 

i made StartGame() static and now it works

GigaPr