AJAX将不会发布到URLAJAX、URL

2023-09-10 21:46:04 作者:第8號當鋪

我试着去建立一个使用表单参数发送到的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.

另请参见http://stackoverflow.com/questions/395045/cross-site-xmlhtt$p$pquest

 
精彩推荐
图片推荐