Solved

retrieve json message

Posted on 2014-02-08
11
293 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 43

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:sevensnake77
ID: 39845958
this is enough information this is returned back from json
0
 
LVL 43

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 9

Author Comment

by:sevensnake77
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 43

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:sevensnake77
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:sevensnake77
ID: 39846245
myid is not displaying at all.
0
 
LVL 43

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:sevensnake77
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 43

Accepted Solution

by:
Chris Stanyon earned 500 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:sevensnake77
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

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.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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…

730 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