Amjad Hawash
Amjad Hawash

Reputation: 21

Send data from PHP to Javascript in different domains

I send data from Javascript to PHP file using AJAX, but I couldn't take response data back from PHP to Javascript, knowing that Javascript code is in different domain than PHP code. Anyone has an idea to solve this problem?

regards,

Upvotes: 2

Views: 173

Answers (3)

Ayush
Ayush

Reputation: 42450

To receive a response in JS cross-domain, you can either enable CORS by adding the Access-Control-Allow-Origin header. However, this can be spotty.

A better solution would be to send back your response encoded as JSONP.

Here's an example using JSONP. The API I'm calling is my own which supports JSONP responses. JSONP Example

In contrast, the following example also works. In this case, I am not using JSONP. This request works because my API is CORS enabled. However, like I said, I have found this to be spotty, and would recommend JSONP.

CORS Example

Upvotes: 3

tgriesser
tgriesser

Reputation: 2808

Cross-domain javascript can be difficult. Take a look at http://easyxdm.net/wp/

Upvotes: 1

FabioCosta
FabioCosta

Reputation: 2749

AFAIK there isn't a way to send ajax request to a different domain for security reasons. I can think of some aproachs to workaround:

  • Call a php proxy that then make the request trough sockets or curl. Example
  • make a flash movie that call it and set a crossdomain.xml file

Upvotes: 0

Related Questions