Your code is actually attempting to make a Cross-domain (CORS) request, not an ordinary POST
.
That is: Modern browsers will only allow Ajax calls to services in the same domain as the HTML page.
Example: A page in http://www.example.com/myPage.html
can only directly request services that are in http://www.example.com
, like http://www.example.com/testservice/etc
. If the service is in other domain, the browser won't make the direct call (as you'd expect). Instead, it will try to make a CORS request.
To put it shortly, to perform a CORS request, your browser:
OPTION
request to the target URLOPTION
contains the adequate headers (Access-Control-Allow-Origin
is one of them) to allow the CORS request, the browse will perform the call (almost exactly the way it would if the HTML page was at the same domain).
How to solve it? The simplest way is to enable CORS (enable the necessary headers) on the server.
If you don't have server-side access to it, you can mirror the web service from somewhere else, and then enable CORS there.