我将拔出所有剩余的头发,因此,如果您知道可能是什么问题,请帮助我。谢谢。我所有的谷歌搜索和搜索都没有得到回报。

我正在尝试使用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)

有什么想法或建议吗?

谢谢

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐