Reputation: 1011
Hi I am getting the following error:
200
SyntaxError: JSON.parse: unexpected character
I have checked my JSON in firebug and it says the following:
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"
Which seems ok to me so i'm not sure where i am going wrong and why i am getting a error
My code is as folows:
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);
}
});
});
Controller:
[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);
}
Any help would be appreciated.
Thanks
Upvotes: 0
Views: 2793
Reputation: 1038710
Your server returns invalid JSON:
callback_dc99fd712fff48d6a56e0d9db5465ac3({"MsgId":"b91949f4-a30e-4f3f-b6e8-f83fc40ada89","Status":2,"CurrentServer":"10.10.143.4"})
This is not JSON. This is JSONP and is used from cross domain AJAX calls. In this case you are not making a cross domain AJAX call so you should remove the callback_dc99fd712fff48d6a56e0d9db5465ac3
wrapper and return valid JSON:
{"MsgId":"b91949f4-a30e-4f3f-b6e8-f83fc40ada89","Status":2,"CurrentServer":"10.10.143.4"}
I guess that the [JsonpFakeFilter]
attribute that you have decorated your controller action with is responsible for wrapping the JSON result with this callback.
So get rid of it and make sure that your server returns valid JSON:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Progress(string msgId, int status)
{
...
}
Upvotes: 1