最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - OPTIONS Request instead of PUT in XMLHTTPRequest - Stack Overflow

programmeradmin1浏览0评论

I trying to do the html+javascript part of a web service in REST. With this code as example:

<!DOCTYPE html> 
<html lang="en"
<script type="text/javascript">
  function testPut( url ){
  var xhr = new XMLHttpRequest();

  xhr.open( 'PUT', url, false );
  xhr.setRequestHeader( 'Content-Type', 'text/plain' );
  xhr.send( null );
  }
</script>
<body>
<form name="test" action="">
    <input type="button" value="Lanceur" onclick="testPut(':8080')" />
  </form>
</body>
</html>

But in the web server (done with web.py) I getting OPTIONS instead of PUT:

111.111.111.111:52014 - - [15/May/2013 17:01:47] "HTTP/1.1 OPTIONS /" - 200 OK

How con I solve it? To remove the OPTIONS request and only send a PUT?

I trying to do the html+javascript part of a web service in REST. With this code as example:

<!DOCTYPE html> 
<html lang="en"
<script type="text/javascript">
  function testPut( url ){
  var xhr = new XMLHttpRequest();

  xhr.open( 'PUT', url, false );
  xhr.setRequestHeader( 'Content-Type', 'text/plain' );
  xhr.send( null );
  }
</script>
<body>
<form name="test" action="">
    <input type="button" value="Lanceur" onclick="testPut('http://fake.es:8080')" />
  </form>
</body>
</html>

But in the web server (done with web.py) I getting OPTIONS instead of PUT:

111.111.111.111:52014 - - [15/May/2013 17:01:47] "HTTP/1.1 OPTIONS /" - 200 OK

How con I solve it? To remove the OPTIONS request and only send a PUT?

Share Improve this question asked May 15, 2013 at 15:08 ZhenZhen 4,2836 gold badges39 silver badges59 bronze badges 1
  • Similar question (here)[stackoverflow./q/8153832/57091]. – robsch Commented Jul 6, 2016 at 11:40
Add a ment  | 

1 Answer 1

Reset to default 16

My guess is your a re making a cross domain request so the browser is sending the OPTIONS request as a 'preflight-request' asking the server for permission to send the PUT request.

To confirm this is the case, look at the headers of the OPTIONS request. You should see these headers:

  • Origin: This indicates the domain your client code is
  • Access-Control-Request-Method: What method you are trying to send
  • Access-Control-Request-Headers: This header will be included if your request includes headers other than Accept, Content-Type, and a couple other that are accepted by default
OPTIONS /cors HTTP/1.1
Origin: http://your_request_domain.
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: ...
Host: server_request_domain.
...

If the server gives permission, you should see these header on the OPTIONS response:

Access-Control-Allow-Origin: <<your domain or *>>
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: <<list of allowed headers>>

See this HTML 5 Rocks Tutorial on CORS for a nice explanation of how Cross-Origin Resource Sharing (CORS) work.

发布评论

评论列表(0)

  1. 暂无评论