I'm using MVC 3. I have a method on the controller that returns a Json object, according to this question it should be returned to me as Json, but I am finding that is not the case: ASP.NET MVC3 - Bug using Javascript
here's the code that I have:
function DeleteItem(obj) {
alert(obj.responseText);
alert(obj.Success);
}
</script>
</head>
<body>
@Ajax.ActionLink("test", "Delete", "Home", new { id = "test" }, new AjaxOptions { Confirm = "Delete?", HttpMethod = "Post", OnComplete = "DeleteItem" });
</body>
And the controller:
[HttpPost]
public ActionResult Delete(string id)
{
return Json(new{Success 开发者_开发技巧= true,objectId = "testing"});
}
The first message box displays the response text which is: {"Success":True, "objectId":"testing"}
the second message box displays undefined
So it is coming back to the client correctly, I'm just not sure how to get it out?
...Stefan
The Ajax.*
helpers never really worked nicely. Try using you using normal Html helpers with jquery:
@Html.ActionLink("test", "Delete", "Home", new { id = "test" }, new { id = "delete" })
and then in a separate javascript file:
$(function() {
$('#delete').click(function() {
if (confirm('Delete?')) {
$.post(this.href, { }, function(result) {
alert(result.Success);
});
}
return false;
});
});
You could rebuild the object like this. It worked for me.
Json: {"message":"hello", "success": true}
function getJsonDetails_OnComplete(res) {
var obj = eval("(" + res.responseText + ")");
alert(obj.message);
};
精彩评论