I created a php page which print this from the database
[{"sha_id":"2","sha_text":"This is 1st sha."},{"sha_id":"4","sha_text":"this is 2nd sha"}]
now i want to extract each variable out of this.. after googling for a while i got this
$(document).ready(function(){
var output = $('#output');
$.ajax({
url: 'http://xxxx.com/android_sha/index.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
$.each(data, function(i,item){
var landmark = '<h1>'+item.sha_id+'</h1>'
+ item.sha_id+'</p>';
output.append(landmark);
});
},
error: function(){
output.text('There was an error loading the data.');
alert("error");
}
});
});
but it always alert error for me. I could be something wrong at $.each(data, function(i,item){ but can’t figure out what should be the correct format.
JSONP isn’t actually JSON. JSONP is a “hack” to get crossdomain data. JSONP is actually JavaScript file.
You need to wrap the JSON in a function call. In your example, you’re sending the
jsoncallbackcallback, so you need to wrap the JSON in the value of that.