RubbleFord
RubbleFord

Reputation: 7636

$.ajax and webmethod/pagemethods

I'm trying to call a pagemethod that doesn't have any parameters, and I can't seem to get it working.

If I have a single parameter in the pagemethod it works fine.

$.ajax({
  type: "POST",
  url: "Default.aspx/getLastCallData",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    alert(msg.d);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert('Couldnt get call data');
  }
});

Any ideas.

Upvotes: 0

Views: 566

Answers (2)

Tim Banks
Tim Banks

Reputation: 7179

Since you are not passing any data, you should still add the data parameter and pass an empty JSON object.

By sending an empty JSON object, jQuery will correctly send the contentType you defined in the $.ajax call. This is a weird quirk that jQuery has that hasn't really been explained.

Add the following parameter:

data: "{}"

So your call should look like:

$.ajax({
  type: "POST",
  url: "Default.aspx/getLastCallData",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    alert(msg.d);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert('Couldnt get call data');
  }
});

Upvotes: 3

peirix
peirix

Reputation: 37731

Have you tried switching POST for GET? Don't know if that makes any difference, but since you're not sending any data, you're not really posting anything, you're just getting data...might be something weird going on there.

Upvotes: 0

Related Questions