www.webdeveloper.com
Results 1 to 3 of 3

Thread: .get() and getJson() cross-domain issues.

  1. #1
    Join Date
    Aug 2011
    Posts
    2

    .get() and getJson() cross-domain issues.

    The following topic had driven me nuts for a few hours. I have been reading article after article trying to get this to work, with no luck. Lets start with what im trying to do.


    I am making a JS file that people can link on their webpage, and will create tooltips on links (from data from my database). Ok np.

    I have a php file for testing.

    Code:
    $item = $_GET['item'];
    if ($item == "test") {
    	echo "{test:<div><b> Item of 'hello world'</b> </div>}";
    }
    Ok great, however, when I call using .get it does not work, however, the same goes for .getJson still no go.

    Heres the kicker. I know for a fact .get() does work cross domain. Check here: http://jsfiddle.net/Xj234/ as you can see, it pulls for jsbin.

    Any idea how this is possible? Thank you so much for your time.

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    Edit your jsfiddle to try and grab money.cnn.com, now try webdeveloper.com.

    If you check the console you'll see this:

    Code:
    XMLHttpRequest cannot load http://money.cnn.com/. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.
    
    XMLHttpRequest cannot load http://webdeveloper.com/. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.
    You can configure specific pages using headers (explained a little better here) to allow cross-domain requests to collect the data from pages on a separate domain. I think that jsbin has either a) headers set up allowing jsfiddle requests or b) headers set up allowing requests from any domain.

    Easiest solution is to try server-side proxying - use Javascript to make an AJAX request to a script on your server that makes a cURL (or similar) request to the domain that you want that will return the data to the script on your server which will be returned from your server to the client that made the AJAX request.

    I'm not saying that's the ideal solution (it may take too long to be of any value for you), I'm just saying it's the easiest.

  3. #3
    Join Date
    Aug 2011
    Posts
    2
    ahh, thank you for that. I actually tried use the header trick, but jsfiddle.com not knowing the address was http://fiddle.jshell.net. That would solve it. [Though I figured * would allow all sites.. but did not seem to work]

    However until then, Ive switched my output to JSONP and have it working (which is grand) but im going to tr working with the header again as I would just like to grab the full page.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles