Why is $.post() subject to same-origin policy, but submitting a form with method='POST' okay?

不羁岁月 提交于 2019-11-28 21:24:21
tkone

$.post uses xmlhttprequest to send data. Xhr is restricted under the same-origin policy. Sending a straight up HTTP POST request is not.

When performing a POST request to another domain, you won't be able to access the response with JavaScript (even if you submit the form to an iframe).

When using XHR however, you have full access to the response, so you could do many bad things - e.g. accessing pages where the user is logged in, snooping around in his corporate intranet etc.

So the XHR restrictions are not to avoid CSRF but to avoid disclosure of privileged information.

The ajax same-origin policy is to stop sending your information over the net to their personal server, without you being aware of it happening. Page posts to other servers are not recommended, and may fail.

Can you resolve it? You can replace the form submit with jq to wait for the login status to be completed, and then submit the form, however, I am not sure this is a good idea - a spin loop usually indicates a design error.

How much control over the code do you have? Can you make the submit do a login, and on return send the search? Or make the search not require the login?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!