0

您好我收到以下错误:

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"

这对我来说似乎没问题,所以我不确定我哪里出错了,为什么我会出错

我的代码如下:

查询:

$(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);
        }

任何帮助,将不胜感激。

谢谢

4

1 回答 1

1

您的服务器返回无效的 JSON:

callback_dc99fd712fff48d6a56e0d9db5465ac3({"MsgId":"b91949f4-a30e-4f3f-b6e8-f83f‌​c40ada89","Status":2,"CurrentServer":"10.10.143.4"})

这不是 JSON。这是JSONP,用于跨域 AJAX 调用。在这种情况下,您没有进行跨域 AJAX 调用,因此您应该删除callback_dc99fd712fff48d6a56e0d9db5465ac3包装器并返回有效的 JSON:

{"MsgId":"b91949f4-a30e-4f3f-b6e8-f83f‌​c40ada89","Status":2,"CurrentServer":"10.10.143.4"}

我猜[JsonpFakeFilter]你用这个回调来装饰你的控制器动作的属性负责包装 JSON 结果。

所以摆脱它并确保您的服务器返回有效的 JSON:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Progress(string msgId, int status)
{
    ...
}
于 2013-03-11T12:06:08.077 回答