Reputation: 1119
I have to call to a web service from javascript using ajax:
$.ajax({
type: "GET",
url: "http://[REMOTE-SERVER-IP]:8080/api/service",
contentType: "application/jsonp",
crossDomain: true,
success: successFunc,
error: errorFunc
});
I read that to grant access to the method, a "crossdomain.xml" must be created in the server http://[REMOTE-SERVER-IP]:8080/crossdomain.xml:
<cross-domain-policy>
<allow-access-from domain="[SERVICE-CALLER-IP]"/>
</cross-domain-policy>
But after doing that, when I try to call to the method, I'm getting this error from the javascript debugger:
XMLHttpRequest cannot load http://[REMOTE-SERVER-IP]:8080/[URL]. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin
What am I doing bad?
Thank you very much!!!
Upvotes: 1
Views: 10549
Reputation: 3246
You can prefer two options here and both assumes that you can access the server.
The first option is to add callback=?
parameter to request url, and modify the server response. Server should add the callback function to the response in the following format
callback_function_coming_from_url([your_server_response])
The second option is to add Access-Control-Allow-Origin: *
header to your server response. Or you can specify the address like Access-Control-Allow-Origin: [your_client_address]
I prefer to option 2 since it is the convenient way to accomplish your task, and also, you can control your server response, much more secure than the option 1.
You can get additional info from CORS
Upvotes: 4