PDA

View Full Version : Resolved local vs external getJSON reference



ggalan
09-23-2011, 06:30 PM
i am using xampp to test localhost, parsing a json file.
when i point to an external json file i get an error in httpfox NS_ERROR_DOM_BAD_URI.
when the json file is local or pointed thru localhost everything works fine.
when the same external reference method is placed in my godaddy server it works fine.
why does localhost not accept the external json url?



$(document).ready(function(){
//var url='data.json';//works on everywhere
//var url='http://localhost/data.json';// works localhost
var url='http://www.myurl.com/data.json';//localhost error, only works on live server
$.getJSON(url, function(data){
$.each(data.data, function(i, entry) {
$("p:last").append(entry.img + '<br/>');
});
});
});

traq
09-24-2011, 05:19 PM
NO javascript works cross-domain. It's a basic security precaution. you can set things up to work around it, but it sounds like your script will work "as-is" once you install it on the live server.

ggalan
09-24-2011, 06:02 PM
i see, then why would this work on a live server and not on xampp? is it a setting in php.ini?

djr33
09-24-2011, 06:19 PM
This is a limit with Javascript, and has nothing to do with PHP.

Javascript only works on the same domain, so you can't use your local computer and the remote website at the same time, but if it is running on your live server and requesting information from the same server, it will work.


If you want this to work locally, you will need to change the URL you are requesting to a local file.

traq
09-24-2011, 06:47 PM
exactly; "localhost" and "www.example.com" are different domains, so javascript requests are blocked.

ggalan
09-24-2011, 07:19 PM
ooooh, i always thought this was doing the same thing


<script src="http://code.jquery.com/ui/1.8.14/jquery-ui.min.js"></script>

but i guess this isnt calling a JS function


re: this helped
http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php

lings24
06-15-2012, 07:21 PM
Hey, this is my code. It works locally but not on the server. Where am i going wrong.? Please let me know. Thanks!

$(document).ready(function () {
var myUrl = "json/data.json";

$.getJSON(myUrl, function (data) {
var items = data.results;
var i = 1;

$.each(items, function (index, item) {

$("#mname" + i).append(item.mallName);
$("#mdetails" + i).append(item.deals);
i++;

});
});
});