I call a PHP script on my other server to pull database info and it returns a JSON array. But ajax calls cant call cross domain so I’m running it through a local PHP script that makes a CURL call to the remote PHP script. That script returns a JSON array to the curl script (getURL.php) and that echos back. writing the data returned to the console looks correct but it was counting EVERY character as an array element. So I thought maybe it was coming back as text. But adding $.parseJSON(data) throws an error. Removing it says arr length is 170. Any ideas?
function getPrograms() {
var data = "url=http://www.wdctravel.com/co-op/getPrograms.php";
$.ajax ({
url: "getURL.php",
data: data,
success: function(data) {
var arr = $.parseJSON(data);
postPrograms(arr);
}
});
}
function postPrograms(arr) {
var len=arr.length;
alert (len);
for(var i = 0; i < len; i++) {
if (i % 2 == 0) {
$('#programs tr:last').append("<tr></tr>");
}
$('#programs tr:last').append('<td><input type="text" size="3" name="CoOpID~' + arr[i]['id'] + '" /></td><td>' + arr[i]['name'] + ' <span style="color: red;"><span id="cs' + arr[i]['id'] + '">' + arr[i]['remain'] + '</span> spots left.</span></td>');
var c = $("#programs tr:last td").length;
};
}
Check if your JSON response is malformed.
http://api.jquery.com/jQuery.parseJSON/
If you have control over the remote server, I’d suggest using JSONP.
http://en.wikipedia.org/wiki/JSONP