我在下面的ajax调用中遇到了解析错误,可能出错了什么?
<script type="text/javascript" src="json.js"></script>
var contact1 = {
"id":"5",
"name": "fsdfsd"
};
var jsonString = "{\"JsData\":" + JSON.stringify(contact1) + "}";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/ReceiveData",
data: jsonString,
// data: DTO,
dataType: "json",
success: function(msg) {
alert(msg);
},
error: function(response,ajaxOptions, thrownError) {
alert("error:" + ajaxOptions);
}
});
我的服务器端webmethod是
[System.Web.Services.WebMethod]
public static string ReceiveData(contact1 JsData)
{
//JsonTextParser parser = new JsonTextParser();
//JsonObject obj = parser.Parse(JsData);
//foreach (JsonObject field in obj as JsonObjectCollection)
//{
// string v = field.ToString();
//}
return "success";
}
public class contact1
{
public int id;
public string name;
}
答案 0 :(得分:2)
我得到了解决方案。我使用的是2.0框架,我忘了在web.config中添加Ajax相关的引用。我手动添加了这些,&amp;它奏效了。