treehouse : what would you like to learn today?
Web Design Web Development iOS Development

JSONP Cross Browser Issues

  • I am having tons of trouble trying to pull cross-domain json data. It works perfectly when the json is local, but as soon as I try the same code cross domain, it breaks. I must be having issues with JSONP but not sure what it is... can anyone help?

    Here is the JSON Code

    knoxStaff({
    "name": "John Smith",
    "age" : "34",
    "sex": "male"
    })
    

    and the javascript is

    $.ajax({
    url: 'http://knoxyouthgroup.org/example.php',
    dataType: 'jsonp',
    jsonpCallback: 'knoxStaff', // specify the callback name if you're hard-coding it
    success: function(staff){
      document.write(staff.name);// we make a successful JSONP call!
    }
      });
    

    I left up the json file on the server in case anyone wants to try to pull it successfully. THANKS for anyone's time in advance, I just can't crack this one!

  • It has something to do with enabling a callback, but that's as far as my knowledge goes. Generally you'd include it on the url itself: http://knoxyouthgroup.org/example.php&callback=jsonp

    That's a crude example.

  • Thanks so much for your response Doc... I tried an example of that route too:

    $.getJSON("http://www.knoxyouthgroup.org/example.php?callback=?", function(data) {
    document.write(data.name);// we make a successful JSONP call!
          });
    

    But that came up empty too. Driving me nuts! Is anyone able to make this work or guide me in the right direction?

  • I just tried your example link and it worked perfect for me, what browsers are you testing in?

  • The link to the data works, but trying the jquery to pull in the data displays nothing... or did that part work for you??? That would be great news, but I am not seeing it here. Have tried Chrome, Firefox and Safari.

    I managed to get rid of all errors on the console, but it just doesn't display the data. The document.write function does not seem to be doing anything, just a blank page.

    Thanks for your help SgtLegend, would really love to figure this out!