• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

retrieve json message

what to display information below from json data.

Sort by key
0	"The field Status must b...maximum length of '14'."

Open in new window


 .error(function () {
                var text = json[0].html
                $('#message').html(text);

Open in new window

0
Seven price
Asked:
Seven price
  • 6
  • 5
1 Solution
 
Chris StanyonCommented:
There's not enough code or information to give you an answer.

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
0
 
Seven priceFull StackAuthor Commented:
this is enough information this is returned back from json
0
 
Chris StanyonCommented:
Well it's not valid JSON, and your script is not complete, so unless you can show us more, we can't help!
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Seven priceFull StackAuthor Commented:
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;
    });

Open in new window

Json return data
{"myid":["The field Status must be a string or array type with a maximum length of '14'."]}

Open in new window

0
 
Chris StanyonCommented:
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:

$("#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 ); 
		});
});

Open in new window

0
 
Seven priceFull StackAuthor Commented:
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.
0
 
Seven priceFull StackAuthor Commented:
myid is not displaying at all.
0
 
Chris StanyonCommented:
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/ChrisStanyon/LGzwL/
0
 
Seven priceFull StackAuthor Commented:
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.
0
 
Chris StanyonCommented:
When you make an AJAX call to your server - one of 2 things will happen. The call will either succeed or it will fail.

If it succeeds, then the success() function will be sent the data that your server outputs. This data can be whatever you want it to be - a single text value, a chunk of HTML, or a JSON object containing lots of information, and you can do what you like with it - alert it, drop it into your page, etc.

If the AJAX call to your script fails, for example the page can't be found or your server has a fault, then the error function will be passed the reason for failure (Server Error / Not Found etc.). It can NEVER receive the JSON back from the server, because your script failed to talk to the server.

You can only ever receive proper data back from your server if the AJAX call is successful, and you handle that data in the success handler - the done() function in the code above.

In the code I posted, the AJAX call is made to the server, and your server script sends back a JSON string. That JSON string has 1 property called myid, and the value of that property is an array, containing 1 string - your message. It's entirely up to you what your server sends back to the script.

If everything is working OK, then your data will be available in the success handler

If something is broken on your server, then you will get the reason inside the error handler.

I hope that makes sense, because I can't really explain it any clearer.
0
 
Seven priceFull StackAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now