您好我收到以下错误:
200
SyntaxError:JSON.parse:意外字符
我已经在firebug中检查了我的JSON,并说明了以下内容:
jquery-1.8.3.js (line 2)
POST http://localhost:1579/Comets/Progress/4c691777-2a9f-42ca-8421-d076ab4d0450/1
200 OK
JSON
Sort by key
MsgId "4c691777-2a9f-42ca-8421-d076ab4d0450"
Status 2
CurrentServer "10.10.143.4"
这似乎对我好,所以我不确定我哪里出错了,为什么我会收到错误
我的代码如下:
Jquery的:
$(document).ready(function Progress() {
var msgId = $('textarea.msgId').val();
var status = $('textarea.status').val();
$.ajax({
type: 'POST',
url: "/Comets/Progress/" + msgId + "/" + status,
success: function (data) {
//update status
alert("does this work");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
控制器:
[JsonpFakeFilter]
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Progress(string msgId, int status, String callback)
{
//todo need to put recursive function on here (status)
//check the ip - has it changed
string strHostName = System.Net.Dns.GetHostName();
var ipHostInfo = Dns.Resolve(Dns.GetHostName());
var ipAddress = ipHostInfo.AddressList[0];
var currentServer = ipAddress.ToString();
var cometJson = new CometJson
{
MsgId = msgId,
Status = status,
CurrentServer = currentServer
};
//check what the status is if it is less than 4 we want to add one
if (status <= 4)
{
status = status + 1;
cometJson = new CometJson
{
MsgId = msgId,
Status = status,
CurrentServer = currentServer
};
return Json(cometJson);
}
return Json(cometJson);
}
任何帮助都将不胜感激。
谢谢
答案 0 :(得分:1)
您的服务器返回无效的JSON:
callback_dc99fd712fff48d6a56e0d9db5465ac3({"MsgId":"b91949f4-a30e-4f3f-b6e8-f83fc40ada89","Status":2,"CurrentServer":"10.10.143.4"})
这不是JSON。这是JSONP,用于跨域AJAX调用。在这种情况下,您不进行跨域AJAX调用,因此您应该删除callback_dc99fd712fff48d6a56e0d9db5465ac3
包装并返回有效的JSON:
{"MsgId":"b91949f4-a30e-4f3f-b6e8-f83fc40ada89","Status":2,"CurrentServer":"10.10.143.4"}
我猜你用控制器操作修饰的[JsonpFakeFilter]
属性负责用这个回调包装JSON结果。
所以摆脱它并确保你的服务器返回有效的JSON:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Progress(string msgId, int status)
{
...
}