Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

retrieve json message

Posted on 2014-02-08
11
Medium Priority
?
304 Views
Last Modified: 2014-02-10
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
Comment
Question by:Seven price
  • 6
  • 5
11 Comments
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39845229
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
 
LVL 9

Author Comment

by:Seven price
ID: 39845958
this is enough information this is returned back from json
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39845962
Well it's not valid JSON, and your script is not complete, so unless you can show us more, we can't help!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Author Comment

by:Seven price
ID: 39846106
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
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39846197
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
 
LVL 9

Author Comment

by:Seven price
ID: 39846243
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
 
LVL 9

Author Comment

by:Seven price
ID: 39846245
myid is not displaying at all.
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39846285
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
 
LVL 9

Author Comment

by:Seven price
ID: 39846301
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
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39846319
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
 
LVL 9

Author Comment

by:Seven price
ID: 39847593
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question