1

I have a php file on a server, which just prints random numbers.

I want to get these numbers by using XMLHttpRequest from another domain.

How can I do this?

5
  • not clear .. do you want to send http request from php ? check this php.net/manual/en/function.httprequest-send.php Commented Jan 2, 2010 at 10:32
  • oh sorry forgot to mention, i want to send request from html file (actually for Google chrome extension), if i cant do this, is there any way to do ? Commented Jan 2, 2010 at 10:36
  • have you checked this code.google.com/chrome/extensions/xhr.html Commented Jan 2, 2010 at 10:43
  • oh great, do i have to get it as only JSON ? cant i use just PHP ? like this , xhr.open("GET", "api.example.com/data.php", true); Commented Jan 2, 2010 at 10:47
  • there should not be any problem with that Commented Jan 2, 2010 at 11:01

3 Answers 3

1

You can't.

XHR is subject to the same origin policy. There is ongoing work to design and implement systems to allow cross-domain XHR, but the current state of those (the lack of browser support in particular) make it impractical for any real project.

The usual method to work around this is to implement the system using JSON-P instead of XHR.

Sign up to request clarification or add additional context in comments.

2 Comments

i guess google chrome extensions allow cross-domain with the permission command as i want to send request from html file working in google chrome.
yeah, I agree with David - JSONP, iframe or CSS hacks, or a proxy page (proxy.php?url=someurl) and then handler code on the .php side.
0

You can use Jquery.ajax

http://api.jquery.com/jQuery.ajax/

Comments

0

You can implement CORS (Cross-Origin Resource Sharing) on your server.

The current versions of Google Chrome, Firefox and Safari support it now.

The next versions of Opera and Internet Explorer also support it but have not been released yet as of the beginning of March 2012.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.