heffer86 Posted December 24, 2010 Share Posted December 24, 2010 I have a json script (see below) that only works if my html file and php file are in the same directory. I have the html on one server and the php on another it doesn't work. Is there any reason why this would happen? <html> <head> <title>Parsing JSON From PHP Using jQuery</title> <script src="https://code.jquery.com/jquery-latest.js" type="text/javascript"> </script> </head> <body> <a href="#" id="loaduserdata">User Data</a> <table id="userdata" border="1"> <thead> <th>First Name</th> <th>Last Name</th> </thead> <tbody></tbody> </table> <script> $(document).ready(function(){ $("#userdata tbody").html(""); $.getJSON( "http://mywebserver.com/titanium/index_old.php", function(data){ $.each(data.rows, function(i,user){ var tblRow = "<tr>" +"<td>"+user.title+"</td>" +"<td>"+user.audio+"</td>" +"</tr>" $(tblRow).appendTo("#userdata tbody"); }); } ); }); </script> </body> </html> <!DOCTYPE html> <html> Link to comment Share on other sites More sharing options...
0 AnthonySterling Posted December 24, 2010 Share Posted December 24, 2010 You need to use JSONP with callback for remote server data. See the discussion on the jQuery AJAX manual, namely the DataType section. Note: JSONP is an extension of the JSON format, requiring some server-side code to detect and handle the query string parameter. More information about it can be found in the original post detailing its use. When data is retrieved from remote servers (which is only possible using the script or jsonp data types), the operation is performed using a <script> tag rather than an XMLHttpRequest object. In this case, no XMLHttpRequest object is returned from $.ajax(), and the XMLHttpRequest object and the textStatus arguments passed to the handler functions such as beforeSend will be undefined. The error callback will never be fired for JSONP requests. Link to comment Share on other sites More sharing options...
Question
heffer86
I have a json script (see below) that only works if my html file and php file are in the same directory. I have the html on one server and the php on another it doesn't work. Is there any reason why this would happen?
<html> <head> <title>Parsing JSON From PHP Using jQuery</title> <script src="https://code.jquery.com/jquery-latest.js" type="text/javascript"> </script> </head> <body> <a href="#" id="loaduserdata">User Data</a> <table id="userdata" border="1"> <thead> <th>First Name</th> <th>Last Name</th> </thead> <tbody></tbody> </table> <script> $(document).ready(function(){ $("#userdata tbody").html(""); $.getJSON( "http://mywebserver.com/titanium/index_old.php", function(data){ $.each(data.rows, function(i,user){ var tblRow = "<tr>" +"<td>"+user.title+"</td>" +"<td>"+user.audio+"</td>" +"</tr>" $(tblRow).appendTo("#userdata tbody"); }); } ); }); </script> </body> </html> <!DOCTYPE html> <html>Link to comment
Share on other sites
1 answer to this question
Recommended Posts