搜尋

首頁  >  問答  >  主體

javascript - 發送請求時,時常在發起前多一條請求,會有哪些原因造成?

介面都是CORS跨域

多出的一條請求:Request Method:OPTIONS、沒有傳遞參數

#多出的條請求的response headers

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Methods:POST, GET, PUT, OPTIONS, DELETE
Access-Control-Allow-Origin:*
Access-Control-Max-Age:3600
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Connection:keep-alive
Content-Length:0
Date:Fri, 05 May 2017 09:27:40 GMT
Server:WildFly/10
X-Powered-By:Undertow/1

正常要求的response headers

#
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Methods:POST, GET, PUT, OPTIONS, DELETE
Access-Control-Allow-Origin:*
Access-Control-Max-Age:3600
Connection:keep-alive
Content-Type:application/json;charset=UTF-8
Date:Fri, 05 May 2017 09:27:41 GMT
Server:WildFly/10
Set-Cookie:JSESSIONID=Zv7WiKxKZFNkE6ihLsGfGwYF8ZEFNEFxENl7rNLW.localhost; path=/bridge
Transfer-Encoding:chunked
X-Powered-By:Undertow/1
滿天的星座滿天的星座2775 天前565

全部回覆(4)我來回復

  • 漂亮男人

    漂亮男人2017-05-17 10:05:33

    跨域 post請求,需要option一次

    回覆
    0
  • 巴扎黑

    巴扎黑2017-05-17 10:05:33

    樓上正解,如果是簡單的跨域請求,建議不要使用POST

    回覆
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-17 10:05:33

    你curl一下你的請求,應該就不會有這樣的預請求

    回覆
    0
  • PHPz

    PHPz2017-05-17 10:05:33

    1樓正解,跨域請求會先option一次到服務端,經過服務端驗證通過後,在發送請求

    回覆
    0
  • 取消回覆