[英]AJAX - How to retrieve the returned value from my static [webmethod]
I've got the following PageMethod in my Default.aspx page: 我的Default.aspx页面中有以下PageMethod:
[WebMethod]
public static string Hello()
{
return "Hello";
}
Here I've got a div text of which I want to set to whatever is returned via AJAX: 在这里,我有一个div文本,我想设置为通过AJAX返回的任何内容:
<div id="ajaxDiv"></div>
And below are two ways I invoke AJAX. 以下是我调用AJAX的两种方式。 The first one is the standard AJAX call:
第一个是标准的AJAX调用:
function testAjax()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4 && status == 200)
$('#ajaxDiv').text(xmlhttp.responseText);
}
xmlhttp.open("POST", "Default.aspx/Hello", true);
xmlhttp.send();
} }
And the second one is with the help of JQuery: 第二个是在JQuery的帮助下:
function testAjaxJQuery()
{
$.ajax(
{
type: "POST",
url:"Default.aspx/Hello",
data: "{}",
success: function (data)
{
$('#ajaxDiv').text(data);
}
}
)
} }
I bind a button onclick event to these functions but with both approaches I get the markup of my Default.aspx page, not the word "Hello". 我将按钮onclick事件绑定到这些函数,但是使用这两种方法我得到Default.aspx页面的标记,而不是单词“Hello”。 What else do I have to do.
还有什么我需要做的。
you should use data.d for get response: 你应该使用data.d来获得响应:
$.ajax({
type: "POST",
url: "Default.aspx/Hello",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$("#ajaxDiv").text(data.d);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.