为什么HttpRequest发送OPTIONS动词而不是POST? [英] Why is HttpRequest sending the OPTIONS verb instead of POST?

查看:641
本文介绍了为什么HttpRequest发送OPTIONS动词而不是POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

  var req = new HttpRequest 
req.open(POST,http:// localhost:8031 / rest / user / insert);
req.setRequestHeader(Content-type,application / json);
reqsend(json.stringify(user_map));但是,不是发送POST动词,当我在fiddler看到它时,我看到这个:



 选项http:// localhost:8031 / rest / user / insert HTTP / 1.1 
主机:localhost:8031
连接:keep-alive
访问控制请求方法:POST
原产地:http://127.0.0.1:3030
User-Agent: Mozilla / 5.0(Windows NT 6.2; WOW64)AppleWebKit / 537.33(KHTML,像Gecko)Chrome / 27.0.1430.0(Dart)Safari / 537.33
访问控制请求标头:origin,content-type
Accept:* / *
Referer:http://127.0.0.1:3030/E:/grole/dart/Clases/Clases/web/out/clases.html
Accept-Encoding:gzip, defs,sdch
Accept-Language:es-ES,es; q = 0.8
Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3 $ b $正如你所看到的,它使用OPTIONS动词而不是POST?


$

b $ b

发生了什么?

解决方案

OPTIONS 动词是一些浏览器发送的预检请求,用于检查跨源请求的有效性。它几乎检查服务器, Origin (请求者)被允许请求指定的资源。此外,根据服务器发回的标头,它允许浏览器知道从服务器请求源的哪些头,方法和资源。



浏览器发送OPTIONS请求,然后如果服务器回答正确的头(CORS头)允许原点发出请求,您应该看到您的POST请求之后。



请注意,CORS标头必须在OPTIONS响应以及POST响应中返回。这意味着您的服务器必须能够对您要跨域访问的路由响应选项方法。



这称为跨源资源共享。 Mozilla有一些很好的文档。 https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS



如果您有更多问题,请告诉我,我会回答。


I got this code:

var req = new HttpRequest();
req.open("POST", "http://localhost:8031/rest/user/insert");
req.setRequestHeader("Content-type", "application/json");
req.send(json.stringify(user_map));

But, instead of sending the POST verb, when I see it in fiddler I see this:

OPTIONS http://localhost:8031/rest/user/insert HTTP/1.1
Host: localhost:8031
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://127.0.0.1:3030
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1430.0 (Dart) Safari/537.33
Access-Control-Request-Headers: origin, content-type
Accept: */*
Referer: http://127.0.0.1:3030/E:/grole/dart/Clases/Clases/web/out/clases.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

As you can see, it is using the OPTIONS verb instead of POST?

What's going on?

解决方案

The OPTIONS verb is a preflight request sent by some browsers to check the validity of cross origin requests. It pretty much checks with the server that the Origin(requester) is allowed to make the request for a specified resource. Also, depending on which headers are sent back by the server it lets the browser know which headers, methods, and resources the origin is allowed to request form the server.

The browser sends the OPTIONS request then if the server answers back with the correct headers (CORS headers) allowing the origin to make the request, you should see your POST request go through afterwards.

Note that the CORS headers must be returned on both the OPTIONS response as well as the POST response. This means your server must be able to respond to the options method on the routes you want to access across domains.

This is known as Cross-origin Resource Sharing. Mozilla has some pretty good documentation on the subject. https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

If you have more questions let me know and I'll answer them.

这篇关于为什么HttpRequest发送OPTIONS动词而不是POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆