AJAX将不会发布到URL [英] AJAX won't POST to URL

查看:147
本文介绍了AJAX将不会发布到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去建立一个使用表单参数发送到的tumblr API一个简单的AJAX脚本。

Im trying to build a simple AJAX script that uses a form to send parameters to the Tumblr API.

这是目前的code:

<body>
<script type="text/javascript">
function ajaxFunction()
{
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var title = document.getElementById("title").value;
var body = document.getElementById("body").value;
var str = "&email=" + email + "&password=" + password + "&title=" + title + "&body=" + body;
//document.write(str);
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
document.getElementById("suc").value.innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","http://www.tumblr.com/api/write?generator=TumblWave&type=regular",true);
xmlhttp.send(str);
}
</script>
<form id="myForm">
Name: <input type="text" id="email" />
Password: <input type="password" id="password" />
Title: <input type="text" id="title" />
Body: <input type="text" id="body" />
<input type="submit" onclick="ajaxFunction();" />
<div id="suc"></div>
</form>
</body>
</html>

抱歉的混乱。

Sorry for the mess.

不知何故,XMLHtt prequest似乎并没有渡过难关,因为从来都没有进入的tumblr。然而,当我删除了文件撰写(STR)的意见,好像一切都被牵强,因为它应该从形式和印有正确的命令。

Somehow, the XMLHttpRequest doesn't seem to pull through, since nothing ever enters Tumblr. However, when I remove the comments for "document.write(str)" it seems like everything is being fetched as it's supposed to from the forms and printed with the right commands.

您可能会注意到上xmlhttp.open的URL,我已经包含了两个静态参数:发电机名称和应发送到的tumblr API后的类型

You might notice on the URL on xmlhttp.open that I've already included two static parameters: the generator name and the type of post that should be sent to the Tumblr API.

我在w3schools.com一个模型,只有少量修改后提出这一点。任何帮助AP preciated:)

I made this after a model on w3schools.com with only few modifications. Any help appreciated :)

推荐答案

您不能使用xmlhtt prequest张贴到你正在浏览的网页托管在不同的网站,你只能去相同的URL上。

You can't use xmlhttprequest to post to a different site you can only go to the same url that the page you're viewing is hosted on.

另请参见<一个href="http://stackoverflow.com/questions/395045/cross-site-xmlhtt$p$pquest">http://stackoverflow.com/questions/395045/cross-site-xmlhtt$p$pquest

这篇关于AJAX将不会发布到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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