javascript 无法使用 jquery ajax 调用 aspx 页面 web 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20658042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to call aspx page web method using jquery ajax
提问by iJade
Getting aspx page html when trying to call web method on aspx page.Here is the jQuery code
尝试在 aspx 页面上调用 web 方法时获取 aspx 页面 html。这是 jQuery 代码
$(function () {
$.ajax({
type: 'POST',
url: 'Default.aspx/Hello',
data: "{}",
async: false,
success: function (response) {
console.log('Success: ', response);
},
error: function (error) {
console.log('Error: ', error);
}
});
});
And here is the web method code
这是网络方法代码
[System.Web.Services.WebMethod]
public static string Hello()
{
string returnString = "hoiiiiiii";
return returnString;
}
Can any one point out what may be wrong.
任何人都可以指出什么可能是错误的。
回答by Karl Anderson
Two things:
两件事情:
- You are missing the
contentType
in your jQuery.ajax()
function. You need to account for the
.d
value in the JSON response.$.ajax({ type: "POST", url: "Default.aspx/Hello", data: "{}", async: false, contentType: "application/json; charset=utf-8", dataType: "json", success: function(result) { if (result.hasOwnProperty("d")) { // The .d is part of the result so reference it // to get to the actual JSON data of interest console.log('Success: ', result.d); } else { // No .d; so just use result console.log('Success: ', result); } } });
- 您
contentType
的 jQuery.ajax()
函数中缺少。 您需要考虑
.d
JSON 响应中的值。$.ajax({ type: "POST", url: "Default.aspx/Hello", data: "{}", async: false, contentType: "application/json; charset=utf-8", dataType: "json", success: function(result) { if (result.hasOwnProperty("d")) { // The .d is part of the result so reference it // to get to the actual JSON data of interest console.log('Success: ', result.d); } else { // No .d; so just use result console.log('Success: ', result); } } });
Note: The
.d
syntax was an anti-XSS protection put in by Microsoft in the ASP.NET 3.5 release of ASP.NET AJAX; therefore the check to see if the.d
property is there or not.
注意:该
.d
语法是微软在 ASP.NET AJAX 的 ASP.NET 3.5 版本中引入的一种反 XSS 保护;因此要检查该.d
物业是否在那里。
回答by Venugopal M
You could code like this: (This works for me very well) I used jquery from CDN: "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"
你可以这样编码:(这对我很有效)我使用了 CDN 中的 jquery:“ http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js”
public class MyClass
{
public string myName
{
get { return "Hello"; }
}
}
In your aspx.cs page:
在您的 aspx.cs 页面中:
[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = true)]
public static object MyMethod()
{
return new MyClass();
}
And in your ASPX page:
在您的 ASPX 页面中:
$.ajax({
url: "somepage.aspx/MyMethod",
data: {},
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
success: function (data) {
if (data.hasOwnProperty("d"))
alert(data.d.myName);
else
alert(data);
},
error: function (reponse) {
alert(reponse);
}
});
回答by NiKhil Kutewalla
try this -
试试这个 -
$(function () {
$.ajax({
type: 'GET',
url: 'Default.aspx/Hello',
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (response) {
console.log('Success: ', response);
},
error: function (error) {
console.log('Error: ', error);
}
});
});
and the web method -
和网络方法 -
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string Hello()
{
string returnString = "hoiiiiiii";
return returnString;
}