duplicate ajax calls in angularjs

北慕城南 提交于 2019-12-30 12:52:22

问题


I am using express-jwt to build a restful api. Now the client is making duplicate ajax calls, for the first one the initiator is angularjs and for the second one the initiator is other. The first one gets 204 as the response code and the second one gets 200 as the response code. I tried to debug to get to the source of this duplicate requests, but I am not able to.

Below is the header details for the one with 204 status code

Below is the header details for the one with 204 status code

Can any one suggest what could be the issue?


回答1:


The first call is OPTIONS type. That's a pre-flight call which a browser sends if the page and api are not on same domain.

The purpose of this call is to deal with CORS. Backend usually needs to send the allowed request method types (GET, POST etc.). The browser will then send the real call if the desired request type is among those returned.

Here's a sample of the response headers.

You can ignore it for all intents and purposes. It does not contain any normally useful payload or return data.

Take a look at AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE? for more info.




回答2:


Those two requests are different one is OPTIONS and other is GET.

For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

You need to handle in the server when the request method OPTIONS , then you need to exit with out processing.



来源:https://stackoverflow.com/questions/32735388/duplicate-ajax-calls-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!