Seven price
asked on
retrieve json message
what to display information below from json data.
Sort by key
0 "The field Status must b...maximum length of '14'."
.error(function () {
var text = json[0].html
$('#message').html(text);
ASKER
this is enough information this is returned back from json
Well it's not valid JSON, and your script is not complete, so unless you can show us more, we can't help!
ASKER
Ok I submit the information from a form then on error i return json data from the error I am having. I want the error to return to #message but I get nothing back.
$("#form1").submit(function () {
// This example shows how to send a complex CLR type as HTML form data.
// var errorData = $.parseJSON(xhr.responseText);
var jqxhr = $.post('api/updates/complex', $('#form1').serialize())
.success(function () {
var loc = jqxhr.getResponseHeader('Location');
var a = $('<a/>', { href: loc, text: loc });
$('#message').html(a);
})
.error(function () {
var text = json["myid"]._errors[0]
//alert("test");
$('#message').html(text);
});
return false;
});
Json return data{"myid":["The field Status must be a string or array type with a maximum length of '14'."]}
Right. You will only ever get the JSON data back in the success function. The error function is only fired if your AJAX call fails for some reason (page not found, server error etc.).
You need to tell your post() call that you're expecting JSON back from the server, and then pass the JSON data into the success function.
The way your JSON is formatted means you have a key called 'myid' that contains an array of data with 1 element, so you need to access the key and then the element.
The AJAXRequest object doesn't have a 'location' property, so not sure what you're expecting there.
Finally, in the latest versions of jQuery, the success() and error() methods are deprecated. You should switch to done() and fail()
Have a look at this:
You need to tell your post() call that you're expecting JSON back from the server, and then pass the JSON data into the success function.
The way your JSON is formatted means you have a key called 'myid' that contains an array of data with 1 element, so you need to access the key and then the element.
The AJAXRequest object doesn't have a 'location' property, so not sure what you're expecting there.
Finally, in the latest versions of jQuery, the success() and error() methods are deprecated. You should switch to done() and fail()
Have a look at this:
$("#form1").submit(function (e) {
e.preventDefault();
$.post( 'api/updates/complex', $('#form1').serialize(), null, 'json' )
.done( function ( response ) {
//response now holds the JSON response from your server
$('#message').html( response.myid[0] );
})
.fail( function ( jqXHR, textStatus, errorThrown ) {
//this only gets fired if the AJAX call failed!
alert( errorThrown );
});
});
ASKER
No can you add this on jfiddle. The error is correct I want it to go to the failed. I just want to display it in html in the message id.
ASKER
myid is not displaying at all.
The error function only gets fired if your AJAX call fails - it will NEVER receive JSON from your server!
Here's a working fiddle:
http://jsfiddle.net/ChrisS tanyon/LGz wL/
Here's a working fiddle:
http://jsfiddle.net/ChrisS
ASKER
so the correct terminology is
Get single value from Json response.
you saying this is not possible.
what about a get within the error response.
Get single value from Json response.
you saying this is not possible.
what about a get within the error response.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi chriss Well I understand if a code 400 then I cannot get the json error. But this is mvc in a controller and when I postback a 400 error I do see in the dom the error I listed above comeing back from DataAntoantions. but since it is a 400 it will not allow me to grab it. Thanks
The data you've shown is not valid JSON, and in your error call you are trying to access a variable called json, but haven't shown us where you're setting it