Pregunta

I've seen that angular (1.2.17) is sending an OPTION request if we set some custom headers on another domain in ajax (with $http). I know this is some part of an RFC somewhere (no need to point that out) but I would force to send the actual request anyway because the users will be behind a 3G network somewhere in Africa (and you guess that the OPTION header will not work).

So the problem is definitely on the client side because $.ajax works and not $http. I've seen the other threads in stackoverflow on this issue, but I don't want to configure the server-side, I want to disable it on the client side in angular. (please don't suggest this).

the useXDomain option I've seen does nothing and the domainWhiteList thing does not work either. Is there any way to disable this thing entirely ? (or I will have to use jquery instead for the ajax part).

¿Fue útil?

Solución

It is not AngularJS directly that does the OPTION request, it's done by XHR. You can check the AngularJS source, AngularJS does not explicitly perform an OPTION request.

According to MDN, XHR first does an OPTION request when:

  • It uses methods other than GET, HEAD or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted.
  • It sets custom headers in the request (e.g. the request uses a header such as X-PINGOTHER)

I noticed that AngularJS sets the content type header for POST requests to application/json;charset=utf-8. I would try to set it back to text/plain.

 $httpProvider.defaults.headers.post = {'Content-Type': 'text/plain'}

If this doesn't work, I suggest you check what the differences are between the request headers when a request is made by $.ajax and one made by $http

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top