how to get controller result in javascript
context: onSuccess javascript method after an ajax post
How do I obtain my id in javascript that is sent from my controller's ActionResult?
On the controller I'v开发者_如何学Pythone tried 2 flavors Content Result and JSON Result and both of those show up as [object] in my alert().
Thanks, rodchar
To put it simply, it sounds like you are alerting the AJAX response object, but what you want is actually a property of that object. Without knowing more information (like what JS Library you are using to help make the AJAX call) it's difficult to say more. However, if instead of:
alert(myResponse);
You do:
for (key in myResponse) {
alertInfo += key +"=" + myResponse[key] + "\n";
}
alert(alertInfo)
You'll be able to see the actual properties of your response object. Some of these may have "[object]" as their value, in which case you'd need to do the same trick on them:
for (key in myResponse) {
alertInfo += key +"=" + myResponse[key] + "\n";
if (key == "SOME_OBJECT_KEY") {
alertInfo += "Sub-Values:\n";
for (key2 in myResponse[key]) {
alertInfo += "\t" + key2 +"=" + myResponse[key][key2] + "\n";
}
}
}
and so on and soforth. Of course, as smaclell already a mentioned, a good debugging tool like Firebug can give you that same info with a lot less hassle (just "console.log(myResponse)" and then click on the logged object in the Firebug console).
Not sure what the model is you are sending back but your probably just need to inspect the JSON object you are sending back from the controller. Try using Firebug or another such toolbar to let you inspect the object. Good luck.
精彩评论