www.webdeveloper.com
Results 1 to 15 of 15

Thread: HTTP GET from remote domain

  1. #1
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    223

    HTTP GET from remote domain

    How do you perform a HTTP GET from a remote domain in JavaScript?
    (Ajax requests only seem to work on the same domain)
    (I want to pass one parameter and receive back the HTML)


    Thanks in advance and kind regards

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    When it comes to foreign domains, it (as far as I know) can't be done in JavaScript/AJaX. If it were same domain, it could work. This is better suited for a dynamic scripting language, like PHP, ASP, ColdFusion, JSP, etc.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    the remote site must set the access-control http headers in their html/data response.

    otherwise, you can use YQL's html module to query 3rd party sites for free without needing custom server code.

  4. #4
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    223
    Thanks for both of your replies!

    Incredible, that such a simple wish is so complicated.

    I found this on the net:

    http://www.webresourcesdepot.com/cro...ple-php-proxy/

    How do they do that?
    What do you think of it?
    Is it something similar to the YQL module, that rnd me was mentioning?

    It's worth mentioning that I have all domains under my control.
    So altering the access-control http headers on the server side would not be a problem.
    (I'll be using PHP on the server-side)

    Any other ideas?


    Cheers and kind regards

  5. #5
    Join Date
    Jan 2012
    Posts
    3

    follow up

    Hi arvgta,
    Did you solve this and may I ask how? I am Having the same issue and wonder how to proceed.
    teeb

  6. #6
    Join Date
    May 2010
    Posts
    168
    You could make the request from the same domain server, process the get and return it. If using PHP on the back end, use http_get and perhaps something to parse the reply.

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    if you control the servers, just add the headers.

    YQL is more-or-less a poor-man's simple php proxy. it's nice because it converts things to JSON, but the impossible part that is does is move bits from a restrictred domains into a JSONp url that can be added to any page using <script src=xxx /> tags.

  8. #8
    Join Date
    Jan 2012
    Posts
    3
    Much thanks for your replies. Unfortunately, I do not control the original server which is iframed on my site. I am trying to populate text inside a text input located inside the iframe. I am just beginning to study my first programming language (javascript)so I am familiar with jsonp, <script src=xxx /> and the function callback, rnd me. However, php is something I have never looked at, janusmccarthy. Am I over complicating this or am I at least pointing in the right direction? Thanks again,
    Teeb

  9. #9
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    223
    Hi and thanks for your responses!

    @rnd me: By saying "add the headers" you probably mean something like this? :

    http://enable-cors.org/

    Right?

  10. #10
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    223
    ...by further researching this, I found out, all you have to do seems to be add the following code in PHP:

    header('Access-Control-Allow-Origin: *');

    see: https://developer.mozilla.org/En/Ser...Access_Control

    That's if you control the server, of course...

    Correct?

  11. #11
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    223
    solved by adding:

    header('Access-Control-Allow-Origin: *'); in PHP
    prior to starting the echo(s).
    (just tested it - you can even access the server via AJAX)

    Thanks a million!

    @teeb: Feel free to check out, what's necessary, if you don't control the server like in your case...

    Best of luck!

  12. #12
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    223
    @rnd me: Do you think I could use POST similarly this way, instead of GET?

  13. #13
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by arvgta View Post
    @rnd me: Do you think I could use POST similarly this way, instead of GET?
    yes, even delete and put could work, but it's customizable in the Access-Control-Request-Method header. if you POST anything other than urlformdata, you need that extra header in addition to your allow-origin one...
    also make sure OPTIONS requests work on your server to enable pre-flights.

    see: https://developer.mozilla.org/En/HTTP_access_control

  14. #14
    Join Date
    Jan 2012
    Posts
    3
    Actually,
    There is an Iframe (mypage1) within an Iframe (mypage2) of the origin site. I have access to the two pages with the i-frames only. I don't believe the origin site is jsonp compatible and cookies must be passed. Can headers be used on my 2 iframe pages to fill in form data with in the i-frame? What type of files need the headers in case I can get the origin site to help?
    Thanks for your kind replies,
    teeb

  15. #15
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    223
    Thanks rnd me, very helpful! :-)

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