调用不同域时强制 JQuery 发送 POST 请求而不是 OPTIONS
我需要向不同的域发出 ajax POST 请求(json)(稍后所有内容都将位于同一个域中,但我正在本地主机中进行测试,但还无法访问服务器)。
注意:目前无法修改服务器的代码。
我找到了这个问题的几个解决方案:
jsonp:Doesn't work
CORS plugin for firefox (to force allowed differentdomains):Would work if I send POST request,但 JQuery 本身正在发出第一个 OPTIONS 请求(因为它检测到不同的域,可能)。在这里我遇到了一个问题,因为服务器需要身份验证(对于所有内容,包括选项)。
我在 POST 请求的标头中发送身份验证,但 JQuery 未将其包含在 OPTIONS 请求的标头中,并且由于它是在“后台”完成的,因此我无法添加它。
实际上可能有两种解决方案:
将身份验证标头添加到 OPTIONS 请求中。但正如前面提到的,我自己不会发送此请求,因此我不知道在哪里/如何执行此操作。
强制 JQuery 不发出 OPTIONS 请求,而是直接发送 POST(这是该线程的标题)。我搜索了一段时间但没有找到任何东西。这可能吗?
我正在使用 JQuery 1.7.1 和 $.ajax({...}) 发出请求。
提前致谢。
I need to make an ajax POST request (json) to a different domain (later everything will be in the same domain, but I'm testing in localhost and don't have access to the server yet).
Note: Currently there's not possible to make modifications in the server's code.
I found few solutions for this problem:
jsonp: Doesn't work
CORS plugin for firefox (to force allowing different domains): Would work if I send POST request, but JQuery itself is making first OPTIONS request (because it detects different domains, probably). And here I have a problem, because the server needs authentication (for everything, including OPTIONS).
I am sending the authentication in the header of the POST request, but JQuery is not including this in the header of the OPTIONS request, and since it's being done "in the background", I can't add it.
Actually there could be 2 solutions:
Add authentication header to the OPTIONS request. But as mentioned I'm not sending this request myself, so I don't know where/how to do it.
Force JQuery to not make that OPTIONS request, but send POST directly (which is the title of this thread). I searched a while but didn't find anything. Is that possible?
I'm using JQuery 1.7.1 and $.ajax({...}) to make the request.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个棘手的问题。我建议执行以下操作。
将 ajax 连接创建为同一域上的文件。对位于其他位置的文件进行 cURL 连接。
发送后信息并添加身份验证标头。
您可以在此处查看最后一部分: http://php.bigresource .com/CURL-and-HTTP-Authentication--p8WkKqkq.html
只需谷歌搜索“帖子标题卷曲”应该可以帮助您完成这一部分。
This is a tricky one. I suggest doing the following.
Make a ajax-connection a file on the same domain. Do a cURL-connection to the file located somewhere else.
Send post-information and add authentication headers too.
You can see here for the last part: http://php.bigresource.com/CURL-and-HTTP-Authentication--p8WkKqkq.html
Simply googling "post headers curl" should help you with that part.