In the post How to use Ajax.BeginForm MVC helper with JSON result? Joel references a solution as follows:
function onTestSuccess(data, status, xhr) {
console.log("data", data);
console.log("xhr", xhr);
console.log("status", status);
// Here's where you use the JSON object
//doSomethingUseful(data);
}
How do I reference elements in the JSON object “data” in my code?
My console log shows the following:
LOG: data{“success”:true,”uri”:”/Image/Confirm2?category=foo”}
I am trying to use the value of “uri” in my jquery code. I’ve tried:
console.log(“uri”, data.uri);
but get the folowing as a result:
LOG: datauriundefined
Also for this to work you need to use the
OnSuccess="onTestSuccess"setting in your AjaxOptions when setting up theAjax.BeginForminstead ofOnComplete="onTestSuccess".It turns out that the problem is with your controller action in which you specified incorrect Content-Type of
text/html.So instead of:
you should use: