我即将抓狂,如果你知道问题所在,请帮帮我...谢谢。
我已经搜索了很多,但都没有找到答案。
首先,我正在使用jquery-1.7.2.min.js和ASP.net 2.0 web表单。
我正在尝试使用jquery进行ajax调用,但一直收到语法错误/解析错误的消息。我尝试了很多不同的方式,但当我将dataType设置为json时,它们都会导致错误。
这是我的代码:
我尝试搭建一个web服务,但是结果相同。看起来返回的响应要么是完整的页面HTML,要么是来自web服务的XML,并且由于我将dataType设置为JSON,所以无法成功解析它。 如果是这种情况,我该如何只设置响应返回JSON? 这是我尝试了另外一种方法,但没有成功:
首先,我正在使用jquery-1.7.2.min.js和ASP.net 2.0 web表单。
我正在尝试使用jquery进行ajax调用,但一直收到语法错误/解析错误的消息。我尝试了很多不同的方式,但当我将dataType设置为json时,它们都会导致错误。
这是我的代码:
$.ajax({
type: "POST",
url: "UserList.aspx/GetTestJson",
data: {}, //have also tried "{}" and other options such as removing the line
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function(data, textStatus, jqXHR){
alert('success');
},
//Using below instead of above makes no difference either
//success: function(data){
// alert('success');
//},
error: function(jqXHR, textStatus, errorThrown){
alert('errorThrown: ' + errorThrown);
}
});
以及 aspx 页面方法:
[WebMethod]
public static string GetTestJson()
{
return "Hello My World";
}
我尝试搭建一个web服务,但是结果相同。看起来返回的响应要么是完整的页面HTML,要么是来自web服务的XML,并且由于我将dataType设置为JSON,所以无法成功解析它。 如果是这种情况,我该如何只设置响应返回JSON? 这是我尝试了另外一种方法,但没有成功:
[WebMethod]
public static string GetTestJson()
{
HttpContext.Current.Response.ContentType = "application/json";
string json = JavaScriptConvert.SerializeObject("Hello My World");
return json;
}
我通过jQuery收到的错误信息:
errorThrown.message = "Syntax error"
errorThrown.number = -2146827286
errorThrown.name = "SyntaxError"
textStatus = "parseerror"
jqXHR.status = 200
jqXHR.statusText = "OK"
jqXHR.readyState = 4
jqXHR.responseText = (the complete html of the page)
有任何想法或建议吗?
谢谢