Reputation: 21
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
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.
Upvotes: 3
Reputation: 2808
Cross-domain javascript can be difficult. Take a look at http://easyxdm.net/wp/
Upvotes: 1
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:
Upvotes: 0