我无法从我的visual studio项目中的html页面获得一个简单的ajax示例。它在webform (Aspx)上工作得很好:
..。webform1.aspx和form1.html。
function ShowCurrentTime() {
alert("before json");
$.ajax({
type: "POST",
url: "json.aspx/GetCurrentTime",
data: "{name: bob }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
alert("after json");
}
function OnSuccess(response) {
alert(response.d);
}
..。webform1.aspx和form1.html。
<input id="btnGetTime" type="button" value="Show Current Time"
onclick="ShowCurrentTime()" />
..。json.aspx ...
[WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}
如果我将代码放在webform1.aspx中,它可以正常工作。如果我把它放在form1.html上,json.aspx就什么都不会回来了。我正在使用vs2013在我的机器上以调试模式运行该项目。任何帮助都将不胜感激。
更新#1
我检查了fiddler,服务器返回了以下结果(500):
{“消息”:“无效JSON原语: bob.","StackTrace":”at JSON at .
发布于 2015-07-17 08:04:37
你的儿子畸形了。字符串需要在引号中。把bob
放在引号里,你应该是好的。不过,我不知道它为什么要在ASP.NET页面上工作,除非它有引号。
function ShowCurrentTime() {
alert("before json");
$.ajax({
type: "POST",
url: "json.aspx/GetCurrentTime",
data: "{name: \"bob\" }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
alert("after json");
}
function OnSuccess(response) {
alert(response.d);
}
https://stackoverflow.com/questions/31478396
复制相似问题