39

I would like to parse JSON array data with jquery ajax with the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample</title>
<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
 var result;
 function jsonparser1() {
 $.ajax({
 type: "GET",
 url: "http://10.211.2.219:8080/SampleWebService/sample.do",
 dataType: "jsonp",
 success: function (xml) {
 alert(xml.data[0].city);
 result = xml.code;
 document.myform.result1.value = result;
 },
 });
 } 
</script> 
</head>
<body>
<p id="details"></p>
<form name="myform">
 <input type="button" name="clickme" value="Click here to show the first name" onclick=jsonparser1() />
 <input type="text" name="result1" readonly="true"/> 
</form>
</body>
</html>

My JSON data is:

{"Data": [{"Address":"chetpet","FirstName":"arulmani","Id":1,"LastName":"sathish","City":"chennai"},{"Address":"ramapuram","FirstName":"raj","Id":2,"LastName":"nagu","City":"chennai"},{"Address":"ramapuram","FirstName":"raj","Id":2,"LastName":"nagu","City":"chennai"},{"Address":"ramapuram","FirstName":"ramaraj","Id":3,"LastName":"rajesh","City":"chennai"},{"Address":"ramapuram","FirstName":"yendran","Id":3,"LastName":"sathi","City":"chennai"}],"Code":true}

But i am not getting any output...anybody please help out...

Abdul Munim
19.2k8 gold badges54 silver badges61 bronze badges
asked Jul 31, 2012 at 8:52
3
  • 1
    Maybe it's your backend who doesn't return anything. Post it! Commented Jul 31, 2012 at 8:54
  • Use JSON.parse to parse JSON data. in success: function(data) { var result = JSON.parse(data); document...value = result.Code; } Commented Jul 31, 2012 at 8:55
  • 1
    I wrote an answer for this question here: Loading cross domain html page with jQuery AJAX the last one, supports https Commented Jun 26, 2014 at 16:26

7 Answers 7

92

Concept explained

Are you trying do a cross-domain AJAX call? Meaning, your service is not hosted in your same web application path? Your web-service must support method injection in order to do JSONP.

Your code seems fine and it should work if your web services and your web application hosted in the same domain.

When you do a $.ajax with dataType: 'jsonp' meaning that jQuery is actually adding a new parameter to the query URL.

For instance, if your URL is http://10.211.2.219:8080/SampleWebService/sample.do then jQuery will add ?callback={some_random_dynamically_generated_method}.

This method is more kind of a proxy actually attached in window object. This is nothing specific but does look something like this:

window.some_random_dynamically_generated_method = function(actualJsonpData) {
 //here actually has reference to the success function mentioned with $.ajax
 //so it just calls the success method like this: 
 successCallback(actualJsonData);
}

Summary

Your client code seems just fine. However, you have to modify your server-code to wrap your JSON data with a function name that passed with query string. i.e.

If you have reqested with query string

?callback=my_callback_method

then, your server must response data wrapped like this:

my_callback_method({your json serialized data});
answered Jul 31, 2012 at 9:13
Sign up to request clarification or add additional context in comments.

3 Comments

I wrote an answer for this question here: Loading cross domain html page with jQuery AJAX the last one, supports https
@AbdulMunim: Is it possible to mention the json file in the url property of $.ajax? What are the file extensions supported in url property? is just anything returns with padded js callback?
Is that why I get this error: Uncaught SyntaxError: Unexpected token : in the console? The error has a link which once I click on shows me the JSON data.
8

You need to use the ajax-cross-origin plugin: http://www.ajax-cross-origin.com/

Just add the option crossOrigin: true

$.ajax({
 crossOrigin: true,
 url: url,
 success: function(data) {
 console.log(data);
 }
});
answered Aug 3, 2014 at 21:01

2 Comments

if I got that right it routes things through a third party proxy. convenient, but traffic through a server might defeat the original idea
I have added the same but everytime it gives a failed error. I am using jsonp data type.
1

Your JSON-data contains the property Data, but you're accessing data. It's case sensitive

function jsonparser1() {
 $.ajax({
 type: "GET",
 url: "http://10.211.2.219:8080/SampleWebService/sample.do",
 dataType: "json",
 success: function (xml) {
 alert(xml.Data[0].City);
 result = xml.Code;
 document.myform.result1.value = result;
 },
 });
} 

EDIT Also City and Code is in the wrong case. (Thanks @Christopher Kenney)

EDIT2 It should also be json, and not jsonp (at least in this case)

UPDATE According to your latest comment, you should read this answer: https://stackoverflow.com/a/11736771/325836 by Abdul Munim

answered Jul 31, 2012 at 8:54

5 Comments

What happens if you try to browse directly to the URL?
And what errors do you get in the Error console of your browser?
Its showing there is unexpected token : in JSON data... but its the correct way of usind the arrays in JSON right???
@ChristopherKenney : Its showing error if i put datatype as 'json'. Its ok if i put it as 'jsonp'.
Errors if i use json instead of jsonp: 1)XMLHttpRequest cannot load 10.211.2.219:8080/SampleWebService/sample.do. Origin localhost:3555 is not allowed by Access-Control-Allow-Origin. sample.do 2)GET 10.211.2.219:8080/SampleWebService/sample.do undefined (undefined)
1

Try

alert(xml.Data[0].City)

Case sensitivly!

Jithin Raj P R
6,8569 gold badges41 silver badges71 bronze badges
answered Jul 31, 2012 at 9:00

Comments

0

you need to parse your xml with jquery json parse...i.e

 var parsed_json = $.parseJSON(xml);
answered Jul 31, 2012 at 8:54

1 Comment

Is that in the success function? Because I get an error in the console: Uncaught SyntaxError: Unexpected token : and it displays the error function alert, doesn't even hit the success function.
0

alert(xml.data[0].city);

use xml.data["Data"][0].city instead

answered Jul 31, 2012 at 8:54

Comments

0

use open public proxy YQL, hosted by Yahoo. Handles XML and HTML

https://gist.github.com/rickdog/d66a03d1e1e5959aa9b68869807791d5

answered Sep 16, 2016 at 4:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.