请求方法是OPTIONS而不是post(现有的StackExchange帖子没有帮助) [英] Request method is OPTIONS instead of post (existing StackExchange post doens't help)

查看:122
本文介绍了请求方法是OPTIONS而不是post(现有的StackExchange帖子没有帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您说另一篇文章之前( jQuery $ .ajax(),$。postt在Firefox中发送OPTIONS作为REQUEST_METHOD ),是的,它根本没有帮助,所以..

Before you say there's another post ( jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox ), yeah, it doens't help at all, so..

我正在使用Chrome开发最新版本,当我尝试将文件发送到这样的远程视频转换器服务的API时,它可以工作(一切都在coffeescript中)让我们调用此代码1:

I am using Chrome dev latest version and when I try to send a file to a remote video converter service's API like this, it works (everything is in coffeescript) Let's call this code 1:

  json_string = getNewSignedParams()

  xhr = new XMLHttpRequest
  xhr.open('POST', some_url, true)
  xhr.setRequestHeader("Cache-Control", "no-cache")
  xhr.setRequestHeader("Content-Type", "application/octet-stream")
  xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")
  xhr.setRequestHeader("X-File-Name", some_file.name || some_file.fileName)
  xhr.setRequestHeader("X-Query-Params", json_string)

  xhr.send some_file

以上返回200并且正常工作。但是我在页面上有jQuery,所以我想我会使用它,所以我有上面这样的jQuery版本。我们打电话给这个代码2:

The above returns a 200 and just works. But I have jQuery on the page, so I thought I'll use it so I have a jQuery version of the above like this. And let's call this code 2:

  $.ajax
    url:  some_url
    type: 'post'
    data: some_file
    processData: false 
    beforeSend: (xhr) ->
      xhr.setRequestHeader("Cache-Control", "no-cache")
      xhr.setRequestHeader("Content-Type", "application/octet-stream")
      xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest")
      xhr.setRequestHeader("X-File-Name", some_file.name || some_file.fileName)
      xhr.setRequestHeader("X-Query-Params", json_string)
    success: ->
      console.log 'success'

我得到400(不良请求),说Origin Access-Control-Allow-Origin不允许 http://127.0.0.1:3000

I get this 400 (Bad Request), saying Origin http://127.0.0.1:3000 is not allowed by Access-Control-Allow-Origin.

但是得到这个,如果我取消注释代码1并注释掉代码2并刷新页面并上传文件,这将成功,并注释掉代码1并取消注释代码2并刷新页面并上传文件,NOW code 2 won'抛出400错误请求错误!!

But get this, if I uncomment code 1 and comment out code 2 and refresh the page and upload a file, which will be successful, and comment out code 1 and uncomment code 2 and refresh the page and upload a file, NOW code 2 won't throw that 400 Bad request error!!

但如果我关闭整个浏览器,并加载使用代码2的页面,上传文件将获得400无论我尝试多少次都会出错。然后,如果我执行上一段描述的内容,代码2将起作用!

But if I close the entire browser, and load the page which is using code 2, uploading a file will get me the 400 error no matter how many times I try. Then if I do what the previous paragraph describes, code 2 will work!

还有一件事,Chrome控制台中的网络日志说我用代码发出的请求2有OPTIONS作为请求方法。而在代码1中,请求方法是POST

And one more thing, the Network log in the Chrome console says that the requests I make with code 2 have "OPTIONS" as the request method. Whereas in code 1, the request method is "POST"

有谁知道这里发生了什么?

Does anyone know what's going on here?

推荐答案

这似乎是一个跨站点脚本问题:url可能实际上是问题所在。该URL可能与发出请求的脚本位于不同的主机上。浏览器出于安全原因不允许这样做。

This seems to be a cross-site-scripting problem: The url might actually be the problem. The url is probably on a different host than the script that makes the request. Browsers dont allow that for security reasons.

这篇关于请求方法是OPTIONS而不是post(现有的StackExchange帖子没有帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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