跨域 jQuery 弹出 POST
我正在尝试使用以下 jQuery 弹出窗口进行跨域 POST。 IE 在 jquery.min.js 第 19 行拒绝授予我权限。请注意,我在 www.mysite.com 上运行了以下脚本,并且我正在尝试将数据发布到 www.google.com 上。从而进行跨域发帖。我想这就是 IE 拒绝授予我权限的原因。我知道应该有一个解决方案来创建隐藏的 iframe,但我不知道如何在我的代码中实现它。一些例子将不胜感激。提前致谢!
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/smoothness/jquery-ui.css" type="text/css" />
<style type="text/css">
#register { display: none; }
.ui-dialog { font-size: 10px; }
.ui-dialog .ui-dialog-titlebar-close { visibility: hidden; }
</style>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<script type="text/javascript">
function loadpopup(){
function Init()
{
jQuery('#register').dialog({ width: 400, resizable: false, buttons: { "Continue": function() {
if (jQuery("#email").val().length < 3)
{
alert('Please enter your email address to continue.');
jQuery("#email").focus();
}
else
{
jQuery("#email_2").val(jQuery("#email").val());
$.post("http://google.com/register.php", $("form[name='registerform']").serialize());
jQuery('#register').dialog('close')}
}
}, closeOnEscape: false, modal: true, show: 'slide' });
}
jQuery(document).ready(Init);
}
loadpopup();
</script>
<div id="register">
<form>
<center>
<table><tbody>
<tr><td align="center" style="text-align: center; font-size: 10px; background: #FFFFFF;">
<br><h3>Register</h3>
</td></tr>
<tr><td align="left" style="text-align: justify; font-size: 11px; background: #FFFFFF;">
Please enter your email address to continue.</br></br>
</td></tr>
</tbody></table></center>
<center>
<div>
<table>
<tbody>
<tr><td align="left" style="font-size: 12px; background: #FFFFFF;"><b>Email:</b></td><td align="left" style="background: #FFFFFF; font-size: 11px;"><input type="text" id="email" name="email" maxlength="26"/></td></tr>
</tbody>
</table>
</div>
</form>
</center>
</div>
<form name="registerform" action="http://google.com/register.php" method="post">
<input type="hidden" name="email_2" id="email_2" />
</form>
</body>
</html>
I'm trying to make a cross domain POST using the following jQuery popup. IE gives me permission denied on Line 19 of jquery.min.js. Please note that I have the following script running on www.mysite.com and I'm trying to post the data to www.google.com for example. Thus making a cross domain post. I assume this is why IE is giving me permission denied. I know there should be a solution to create a hidden iframe but I have no idea how to implement this in my code. Some examples would be greatly appreciated. Thanks in advance!
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/smoothness/jquery-ui.css" type="text/css" />
<style type="text/css">
#register { display: none; }
.ui-dialog { font-size: 10px; }
.ui-dialog .ui-dialog-titlebar-close { visibility: hidden; }
</style>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<script type="text/javascript">
function loadpopup(){
function Init()
{
jQuery('#register').dialog({ width: 400, resizable: false, buttons: { "Continue": function() {
if (jQuery("#email").val().length < 3)
{
alert('Please enter your email address to continue.');
jQuery("#email").focus();
}
else
{
jQuery("#email_2").val(jQuery("#email").val());
$.post("http://google.com/register.php", $("form[name='registerform']").serialize());
jQuery('#register').dialog('close')}
}
}, closeOnEscape: false, modal: true, show: 'slide' });
}
jQuery(document).ready(Init);
}
loadpopup();
</script>
<div id="register">
<form>
<center>
<table><tbody>
<tr><td align="center" style="text-align: center; font-size: 10px; background: #FFFFFF;">
<br><h3>Register</h3>
</td></tr>
<tr><td align="left" style="text-align: justify; font-size: 11px; background: #FFFFFF;">
Please enter your email address to continue.</br></br>
</td></tr>
</tbody></table></center>
<center>
<div>
<table>
<tbody>
<tr><td align="left" style="font-size: 12px; background: #FFFFFF;"><b>Email:</b></td><td align="left" style="background: #FFFFFF; font-size: 11px;"><input type="text" id="email" name="email" maxlength="26"/></td></tr>
</tbody>
</table>
</div>
</form>
</center>
</div>
<form name="registerform" action="http://google.com/register.php" method="post">
<input type="hidden" name="email_2" id="email_2" />
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在通过 XHR 发布到不同的域。由于同源政策,您无法执行此操作。
相反,构建表单,然后对其调用
submit()
。按照您现在的做法,Google 将返回
405 Method Not allowed
,返回该错误的原因有多种。You are posting via XHR to a different domain. You can't do that because of same origin policy.
Instead, build the form and then call
submit()
on it.The way you are doing it now, Google is returning
405 Method Not Allowed
which could be returned for a number of reasons.