1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help get JSON data from remote domain in jquery with jQuery.getJSON

Discussion in 'jQuery' started by bikerjeg, Mar 11, 2010.

  1. #1
    I am trying to parse some JSON formatted data using jquery. I am using json_encode php function to output the json data but I am having trouble getting the jquery syntax correctly to do some cross domain data inclusion on a remote domain from where I am outputting the data.

    This is the JSON data I am outputing:
    ({"links":[{"id":"1","title_attribute":"testing","url":"http:\/\/google.com"},{"id":"2","title_attribute":"testing","url":"http:\/\/google.com"},{"id":"3","title_attribute":"roundtwo","url":"http:\/\/ebay.com"},{"id":"4","title_attribute":"whatwhat","url":"http:\/\/google.com"},{"id":"5","title_attribute":"test445","url":"http:\/\/yahoo.com"}]})
    Code (markup):
    I would like to get each id, title_attribute, and url and add it to a div on a remote domain using jquery.

    Now I am not sure if I am outputting the content correctly or if I am not using the right header when outputting the JSON data in php. I have tried:
    header('Content-type: text/javascript; charset=utf-8;');
    PHP:
    and
    header('Content-type: application/json');
    PHP:
    but no difference. I also tried outputting the following sample data from jquery's jQuery.getJSON function documentation page in php with both headers but I can't access the data using the sample queries from this page http://api.jquery.com/jQuery.getJSON/
    The sample data I tried outputting in php:
    
    {
      "foo": "The quick brown fox jumps over the lazy dog.",
      "bar": "ABCDEFG",
      "baz": [52, 97]
    }
    
    Code (markup):
    Can someone help me get JSON data correctly off of a remote domain? I have exhausted most of my options and I am afraid I may be overlooking something very simple.
    The sample JSON data I am outputting can be directly accessed from the following page http://elado.fanecho.com/index.php/links/get/17

    Thanks.
     
    bikerjeg, Mar 11, 2010 IP
  2. bikerjeg

    bikerjeg Active Member

    Messages:
    364
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    At this point I am willing to pay for help with this. If you are a javascript developer with Jquery experience/knowledge please let me know. Even a hint to help me fix this will deserve re-compensation for their time.
     
    bikerjeg, Mar 16, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    OK, all right, if I have to do this, I should use HTML DOM or something similar to Google hacks: placing php files on my hosting and let them fetch the required data, echo-ing returned result as JSON. Ajax will send requests to myfile.php, not directly to remote host, which will be queried via php class.
    Hope it helps.
    Regards :)
     
    koko5, Mar 16, 2010 IP
  4. inegoita

    inegoita Member

    Messages:
    54
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #4
    Hi

    might be a stupid question, but did you try putting [ and ] instead of ( and ) at the begining/end?

    [{"links":[{"id":"1","title_attribute":"testing","url":"http:\/\/google.com"},{"id":"2","title_attribute":"testing","url":"http:\/\/google.com"},{"id":"3","title_attribute":"roundtwo","url":"http:\/\/ebay.com"},{"id":"4","title_attribute":"whatwhat","url":"http:\/\/google.com"},{"id":"5","title_attribute":"test445","url":"http:\/\/yahoo.com"}]}]


    let me know
    John
     
    inegoita, Mar 16, 2010 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    hello.

    when you do JSONP from a remote host, you typically need to assign a callback function which encapsulates the data object.

    eg, your request can go to:

    http://query.yahooapis.com/v1/publi...l="http://forums.digitalpoint.com"&format=xml&callback=foo

    the resulting data SHOULD be 'wrapped' by the API into a function called foo as the argument:

    foo({...json here });

    so for it to work you define a local function that matches the callback:
    
    window.foo = function(data) {
        console.log(data);
    }
    
    Code (javascript):
    that's about it. i tend to use jsonp a lot for a number of things, have a look at examples i've written here:
    http://fragged.org/tag/jsonp - i know they are for mootools and not jquery but they are similar and can give you ideas.

    posts on using twitter to get latest tweets, tweetmeme to get tweet counts, flikr api, cross-domain ajaxing through yql etc. basically, whatever you can think of.
    when the remote site does NOT support a 'callback=' parameter and just outputs plain JSON, you need to write a php proxy or use YQL to fetch it instead (probably best) and return a callback func.

    good luck - jsonp is a GREAT technology :)
     
    dimitar christoff, Mar 16, 2010 IP
  6. bikerjeg

    bikerjeg Active Member

    Messages:
    364
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Thanks but this is not quite feasible with what I am working with. I am actually trying to build an application to provide users the ability to post the JSON data I am outputting onto their own site. I want to end up providing a simple javascript code they can embed and have the data added to a div on their site. Since not all users have php access on their sites I want to keep it simple and just have them copy something into their html header that will get the job done. I figured jquery would be easiest to implement but I have little javascript experience.
     
    bikerjeg, Mar 17, 2010 IP
  7. bikerjeg

    bikerjeg Active Member

    Messages:
    364
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Just tried this. I even tried a new tutorial from here
    http://blog.reindel.com/2007/10/02/parse-json-with-jquery-and-javascript/
    I replaced the data I am outputting as JSON with the sample data they provide in the .js file and formatted it with the prefixing and suffixing brackets [] take a look here http://elado.fanecho.com/index.php/links/get/17.js

    and replaced the sample code
    $.getJSON("jrss.js", function(json) {
    Code (markup):
    with
    $.getJSON("http://elado.fanecho.com/index.php/links/get/17.js", function(json) {
    Code (markup):
    but the sample code stopped working now that I am trying to pull the data off of the remote domain. Any ideas.
     
    bikerjeg, Mar 17, 2010 IP
  8. bikerjeg

    bikerjeg Active Member

    Messages:
    364
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #8
    THANKS A MILLION FOR THIS SUGGESTION!!

    ...at first I was not clear as to what you were hinting at but I went ahead and searched for more information about JSONP which I was not aware there was even a difference between JSONP and JSON or for that matter that what I was trying to do relied on JSONP. Turns out you were right and it was a simple matter of adding the $_GET['callback'] in my php file and adding the ?callback=? in my Jquery url. Turns out everything else was right and this simple little issue was what was holding me back in getting this app finished. Thanks a lot. You deserve recompense so pm me and I'll paypal you ;).

    Thanks again for the help.

    If anyone is not clear or is having similar issues I suggest you read through this http://www.ibm.com/developerworks/library/wa-aj-jsonp1/ this http://stackoverflow.com/questions/760993/jquery-getjson-cross-domain-problems and this http://stackoverflow.com/questions/753996/jsonp-in-codeigniter
    The last two links pertain more to codeigniter which is what I am using for development but it has a general solution for php that you should be able to understand. Good luck!
     
    bikerjeg, Mar 17, 2010 IP
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    that's ok, keep your money, if you feel i helped, just click the reputation button :)
     
    dimitar christoff, Mar 18, 2010 IP