$ .ajax和webmethod/pagemethods [英] $.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.
如果我在pagemethod中只有一个参数,则可以正常工作.
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');
}
});
任何想法.
推荐答案
由于未传递任何数据,因此仍应添加data参数并传递一个空的JSON对象.
Since you are not passing any data, you should still add the data parameter and pass an empty JSON object.
通过发送一个空的JSON对象,jQuery将正确发送您在$ .ajax调用中定义的contentType.这是jQuery的一个奇怪的怪癖,实际上并没有得到解释.
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.
添加以下参数:
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');
}
});
这篇关于$ .ajax和webmethod/pagemethods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!