Results 1 to 7 of 7

Thread: local vs external getJSON reference

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default local vs external getJSON reference

    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?

    Code:
    $(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/>');
        });
      });
    });
    Last edited by ggalan; 09-25-2011 at 12:21 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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.

  3. The Following User Says Thank You to traq For This Useful Post:

    ggalan (09-25-2011)

  4. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    i see, then why would this work on a live server and not on xampp? is it a setting in php.ini?

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. The Following User Says Thank You to djr33 For This Useful Post:

    ggalan (09-25-2011)

  7. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    exactly; "localhost" and "www.example.com" are different domains, so javascript requests are blocked.

  8. #6
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    ooooh, i always thought this was doing the same thing
    Code:
    <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/ho...jquery-and-php
    Last edited by ggalan; 09-25-2011 at 12:21 AM.

  9. #7
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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++;

    });
    });
    });
    Last edited by lings24; 06-15-2012 at 07:34 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •