为什么$ .post()服从同源策略,但是提交一个form ='POST'的表单好吗? [英] Why is $.post() subject to same-origin policy, but submitting a form with method='POST' okay?

查看:150
本文介绍了为什么$ .post()服从同源策略,但是提交一个form ='POST'的表单好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个基于网络的工具,它简化了我们在我的办公室所做的工作。我们的合作伙伴提供给我们的工具有一个我们整个楼层使用的通用登录名,但是它每隔30分钟就会超时,这令人讨厌必须整天重新登录。



过去我所做的是在我的工具中创建一个隐藏的iframe,通过在页面加载时提交隐藏表单并继续每隔30分钟提交一次表单以防止超时来登录。然后他们可以直接从我的工具(通过另一个可见的表单)将搜索提交给合作伙伴工具。

我想用jQuery $ .post()都可以摆脱隐藏的iframe,并使其成为唯一一次提交登录信息的时间是搜索完成时。这样,它不会在不使用时不断发送请求,但仍然可以运行搜索,而不必担心登录超时。



似乎ajax也是相同的原产地政策阻止了这一点,所以目前我只是打开一个新的命名窗口,然后在目标窗口中一个接一个地提交两个隐藏表单。



这个问题是,如果登录请求没有完成,搜索请求没有通过,他们又被带到登录页面。如果他们关闭窗口并再次搜索,它会起作用,但这也很烦人,只是不如原来的情况。

因此,除了你实际上必须看到页面打开(除非它在隐藏的iframe中)通过 $。post()提交参数和使用POST方法提交表单有什么区别?它们在萤火虫中看起来完全相同。有什么办法可以在表单提交中设置回调,因此它在开始第二个请求之前等待第一个请求完成?

解决方案

$。post使用xmlhttprequest发送数据。 Xhr是通过cors限制的。直接发送HTTP POST请求不是。

I'm working on a web-based tool which streamlines the work we do at my office. The tools provided to us by our partner have a generic login that our entire floor uses, but it times out every 30 minutes, which is annoying to have to log-into again all day.

What I had done in the past, was create a hidden iframe inside my tool which logs into it by submitting a hidden form on page load, and continuing to submit the form every 30 minutes to prevent a timeout. They can then submit searches to the partner tool directly from my tool (via another, visible form).

I'd like to use jQuery $.post() to both get rid of the hidden iframe, and make it so the only time it submits the login info is when a search is being done. That way it's not constantly sending requests when not in use, but you can still run a search without having to worry about the login timing out.

It would seem the ajax same origin policy is preventing this, so at the moment I'm just having it open a new named window, and then submitting two hidden forms in the target window one after the other.

The problem with this is if the login request hasn't finished, the search request doesn't go through, and they're brought to the login page again. If they close the window and search again it will work, but this is also annoying, just not as much as the original situation.

So other than the fact that you actually have to see the page open up (unless it's in a hidden iframe) what is the difference between submitting parameters via $.post() and submitting a form using the POST method? They look identical in firebug. Is there any way I can set up a callback on the form submission, so it waits for the first request to complete before starting the second?

解决方案

$.post uses xmlhttprequest to send data. Xhr is restricted via cors. Sending a straight up HTTP POST request is not.

这篇关于为什么$ .post()服从同源策略,但是提交一个form ='POST'的表单好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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