Web Analytics Made Easy -
StatCounter .get() and getJson() cross-domain issues. - CodingForum

Announcement

Collapse
No announcement yet.

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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • jQuery .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
    Not sure if you came across this already and it didn’t work out for you but a quick internet search with the keywords “jquery get cross comain” brought me this as first result: http://james.padolsey.com/javascript...s-with-jquery/
    Stop solving problems you don’t yet have!

    Comment


    • #3
      The server itself is responsible for deciding whether to allow requests from other domains or not. In the case of jsbin.com, it sends the header Access-Control-Allow-Origin:*, which basically means that it is okay with cross domain requests.

      If a server is not ok with cross domain requests, there's nothing you can do from the client side. What you *can* do is fetch the contents from another server which is okay with cross domain requests. In VIPStephan's example, that's the YQL Server. You can also use the server on which the website generating the request resides — in this case, it's not a cross domain request at all.

      Notice that using a proxy server for cross domain requests will always introduce a delay.
      .My new Javascript tutorial site: http://reallifejs.com/
      .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
      .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

      Comment


      • #4
        if you are the one giving it away, why not serve json(p) or js so that all browsers can embed it anywhere?

        in other words, you say you are making a js file, but your php outputs html, not valid js.
        serve valid js, and you won't have a problem because you can forget ajax and use <script> tags to fetch the content.

        this can be as complex as dom-building html and appending it, as flexible as sending json (or a string literal of html) to a pre-defined callback, or as simple as a document.write("<div id='embededContent'>html</div>").

        there are ups and downs to all methods, take the time to figure out the best one for your particular application.

        Code:
        $item = $_GET['item'];
        if ($item == "test") {
        	echo "alert(\"{test:<div><b> Item of 'hello world'</b> </div>}\") ";
        }
        Last edited by rnd me; Aug 21, 2011, 02:23 AM.
        Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

        Comment


        • #5
          Originally posted by VIPStephan View Post
          Not sure if you came across this already and it didn’t work out for you but a quick internet search with the keywords “jquery get cross comain” brought me this as first result: http://james.padolsey.com/javascript...s-with-jquery/
          Yeah thats one of the first things I read... seemed to have no effect.

          Originally posted by venegal View Post
          The server itself is responsible for deciding whether to allow requests from other domains or not. In the case of jsbin.com, it sends the header Access-Control-Allow-Origin:*, which basically means that it is okay with cross domain requests.

          If a server is not ok with cross domain requests, there's nothing you can do from the client side. What you *can* do is fetch the contents from another server which is okay with cross domain requests. In VIPStephan's example, that's the YQL Server. You can also use the server on which the website generating the request resides — in this case, it's not a cross domain request at all.

          Notice that using a proxy server for cross domain requests will always introduce a delay.
          See, I figured that would be the best solution. [as its how jsfiddle was able to do it] However, when I tried to set my header in the php file... I seemed to have no luck (it still would not work) any chance you could give an example? [according to the article I read it should just be as simple as added it to the header() but /shurgs.

          Originally posted by rnd me View Post
          if you are the one giving it away, why not serve json(p) or js so that all browsers can embed it anywhere?

          in other words, you say you are making a js file, but your php outputs html, not valid js.
          serve valid js, and you won't have a problem because you can forget ajax and use <script> tags to fetch the content.

          this can be as complex as dom-building html and appending it, as flexible as sending json (or a string literal of html) to a pre-defined callback, or as simple as a document.write("<div id='embededContent'>html</div>").

          there are ups and downs to all methods, take the time to figure out the best one for your particular application.

          Code:
          $item = $_GET['item'];
          if ($item == "test") {
          	echo "alert(\"{test:<div><b> Item of 'hello world'</b> </div>}\") ";
          }
          Well JOSNP Is what im going with right now... [its the only reason im returning josn instead of only what I wanted too in the first place] the reason I couldnt get the josnp to work was I was not sending the callback=? at the end of the url, and sending it back at the start of the json line... so as of right now I am useing .getJSON and it is working... but I would like to get good old ".get()" to work by getting my header to accept all.



          Thank all three of you for your replies.

          Comment


          • #6
            Originally posted by molster View Post
            See, I figured that would be the best solution. [as its how jsfiddle was able to do it] However, when I tried to set my header in the php file... I seemed to have no luck (it still would not work) any chance you could give an example? [according to the article I read it should just be as simple as added it to the header() but /shurgs.
            It actually is as simple as that. As with all headers, you have to set it before there has been any output, of course. If it's not working for you, you would have to show a live example of your non-working code, so we can have a look whether the header is actually being sent or what else might be wrong.
            .My new Javascript tutorial site: http://reallifejs.com/
            .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
            .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

            Comment

            Working...
            X