duplicate ajax calls in angularjs

2020-03-30 00:17发布

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.

Screen shot

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

204 response code

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

200 response code

Can any one suggest what could be the issue?

2条回答
欢心
2楼-- · 2020-03-30 00:23

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. enter image description here

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.

查看更多
SAY GOODBYE
3楼-- · 2020-03-30 00:37

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.

查看更多
登录 后发表回答