Solved

retrieve json message

Posted on 2014-02-08
11
286 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:sevensnake77
  • 6
  • 5
11 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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:sevensnake77
Comment Utility
this is enough information this is returned back from json
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Well it's not valid JSON, and your script is not complete, so unless you can show us more, we can't help!
0
 
LVL 9

Author Comment

by:sevensnake77
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Author Comment

by:sevensnake77
Comment Utility
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:sevensnake77
Comment Utility
myid is not displaying at all.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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:sevensnake77
Comment Utility
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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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:sevensnake77
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now