【发布时间】:2013-08-22 15:05:20
【问题描述】:
我开始将东西切换到[WebMethod]s,但开始遇到麻烦,并决定我需要对如何正确使用它们进行更多研究。
我将以下内容放入$(document).ready()
$.ajax({
type: "POST",
data: "{}",
url: "http://localhost:3859/Default.aspx/ajxTest",
dataType: "json",
success: function (msg, status) {
alert(msg.name + " " + msg.value);
},
error: function (xhr, status, error) {
var somethingDarkside; //only to put a breakpoint in.
alert(xhr.statusText);
}
});
我的[WebMethod]
[WebMethod]
public static string ajxTest()
{
JavaScriptSerializer ser = new JavaScriptSerializer();
DummyString dum = new DummyString("Please","Work");
string jsonString = ser.Serialize(dum);
return jsonString;
}
我从来没有得到“请工作”。我得到“未定义的未定义”,我可以在 VS 中调试并且调用进入[WebMethod],返回字符串对于 JSON 来说看起来是正确的,但我还没有能够成功调用它。我有解析错误,传输错误。一切都是不一致的,但从来没有什么是对的。我今天有多达 47 个不同的博客、SO 帖子和 google 组标签,我无法完全破解它。
xhr.statusText 状态与发布的一样。我收到status 解析错误。我迷路了。
提前致谢。
编辑:jQuery 1.9.1
EDIT2:DummyString 对象
public class DummyString
{
public string name { get; set; }
public string value { get; set; }
public DummyString(string n, string v)
{
name = n;
value = v;
}
}
编辑 3:我也有 <asp:ScriptManager EnablePageMethods="true" ...
【问题讨论】:
-
结果字符串的内容究竟是什么是?
-
你在浏览器中调试过吗?
-
浏览器调试是我得到
status解析错误的原因。DummyString就像字典一样工作。它返回一个name:please, value:work事物。
标签: c# asp.net jquery webmethod