我需要知道以下两者之间的确切区别:
<form method="POST" action="https://mywebsite/signon.php">
<input name="harv_acc" value="940322903" type="hidden" />
<input name="harv_eml" value="a@b.com" type="hidden" />
<input type="submit" value="SignOn" />
和
var url = "https://mywebsite/signon.php";
$.ajax({
url: url,
type: 'POST',
//dataType: 'html', -- this was something I tried later
//data: "harv_acc=" + accountnumber + "&harv_eml=" + email , this is also what I tried last but below is what I tried first
data: { harv_acc: account, harv_eml: email },
success: function (data) {
closePopup("div_PleaseWait");
alert(data);
//window.location = encodeURI('<%= Url.Action("DownloadDocument", "Documents") %>?DocumentID=' + documentID + '&DownloadType=' + downloadType + '&DownloadPath=' + data);
}
});
当我发布后者时,我收到 200 但没有回复。如果我提交第一个,我会得到正确的回复。
最佳答案
来自评论:
I'm posting to another site
啊哈!这是你的问题。出于安全原因,浏览器会阻止 AJAX 访问外部网站。抱歉,您不会通过 XHR 请求发出该请求。
如果其他网站希望您与他们交流,他们可以通过 JSON-P 公开网站的这一部分,其工作方式如下:
- 我的站点添加
<script src="http://othersite.com/signon.js?username=foo&password=bar&callback=myCallback">
到源代码(是的,为此使用 GET 很麻烦,但 JSON-P 不能以任何其他方式工作),并创建一个名为myCallback
的函数处理响应数据。 - 其他站点登录,然后返回类似
myCallback({success: false, errorMessage: "Incorrect password, try again!"})
的内容 - 该脚本在我的网站上运行,调用
myCallback
,一切都很开心。
JSON-P 是一个强大的协议(protocol),但只有在远程站点同意它的情况下才能工作。尽管如此,如果他们这样做了,jQuery 有一个很好的快捷方式:只需设置 dataType: "jsonp"
它将为您处理整个回调。
但是,如果您没有密切参与该网站,则不太可能发生这种情况,并且您可能不得不放弃这种跨站点交互。抱歉,这种跨域策略对在线安全至关重要。 (我不希望其他网站代表我向 bankofamerica.com
发出请求,kthx。)
关于php - 表单帖子和 jquery 帖子之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11264405/