Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  128] [ 5]  / answers: 1 / hits: 144166  / 12 Years ago, tue, july 31, 2012, 12:00:00

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...


More From » jquery

 Answers
41

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});

[#83955] Monday, July 30, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikoguym

Total Points: 339
Total Questions: 106
Total Answers: 95

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;