Jquery:POST 表单变量并获取 HTTP 响应? (跨域)

发布于 2024-10-04 11:11:40 字数 645 浏览 0 评论 0原文

我需要向在线服务提交一个网络表单,如果一切顺利,该服务将返回 HTTP 200;如果表单提交不正确,则会返回不同的结果。我想要使​​用 AJAX 来显示漂亮的错误,以通知我的用户他们发送了混乱的信息。

所以我开始做我以前做过的事情,这些在 this 中提到问题,但由于表单提交到了我自己的域之外的另一个域(跨域),没过多久就发现Firefox 发送了 OPTIONS 请求,而不是我期望的 POST

由于这些选项不可用,我进一步搜索并找到了 JSONP 函数,我发现了 不支持 POST 跨域请求

显然,在这之后我的头快要爆炸了。我错过了什么吗?

I need to submit a webform to a online service which returns a HTTP 200 if everything went well and something different if the form was submitted incorrectly. Using AJAX is what I want to make pretty errors appear to inform my users that they have sent messy information.

So I started to do what I have done previosly, which are mentioned in this question but as the form is submitted to another domain besides my own (cross-domain), it didn't take long to find out that Firefox sent OPTIONS request instead of the POST I expected.

With those options being unavailable, I searched further and found out about the JSONP function, which I found out not supporting POST for cross-domains requests.

Clearly, my head is about to implode after this. Have I missed something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌能量女王 2024-10-11 11:11:40

您可能想查看 html5 中的 window.postMessage。甚至还有一个名为 jQuery postMessage 的 jQuery 插件,它试图为旧版浏览器提供支持(通过隐藏的 iFrames)尚不支持它。

但一般来说,不支持通过 ajax 发布到单独的域。另一种(可能更简单)的替代方案是简单地在您自己的服务器上创建一个代理脚本,该脚本获取 ajax 数据并通过curl 向您的外部服务执行 POST,然后将结果代码返回到您自己的 ajax 调用。由于您发布到自己的域,因此它不会受到跨域限制。

You might want to look at window.postMessage in html5. There's even a jQuery plugin called jQuery postMessage which tries to bring support to older browsers (through hidden iFrames) which don't support it yet.

Generally though, posting to a separate domain isn't supported via ajax. Another (possibly easier) alternative is to simply create a proxy script on your own server that takes the ajax data and does a POST via curl to your external service and simply returns the result code back to your own ajax call. It doesn't suffer the cross domain restriction since you're posting to your own domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文