简体   繁体   English

AJAX不会发布到URL

[英]AJAX won't POST to URL

Im trying to build a simple AJAX script that uses a form to send parameters to the Tumblr API. 我正在尝试构建一个简单的AJAX脚本,该脚本使用一种形式将参数发送到Tumblr API。

This is the current 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. 对不起,一团糟。

Somehow, the XMLHttpRequest doesn't seem to pull through, since nothing ever enters Tumblr. 不知何故,XMLHttpRequest似乎没有实现,因为没有任何东西进入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. 但是,当我删除“ document.write(str)”的注释时,似乎一切都从表单中获取,并使用正确的命令进行打印。

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. 您可能会注意到,在xmlhttp.open上的URL上,我已经包含了两个静态参数:生成器名称和应发送到Tumblr API的帖子类型。

I made this after a model on w3schools.com with only few modifications. 我是在w3schools.com上的一个模型上进行了少量修改后制成的。 Any help appreciated :) 任何帮助表示赞赏:)

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. 您不能使用xmlhttprequest发布到其他站点,而只能访问与您正在查看的页面所在的URL相同的URL。

See also Cross-site XMLHttpRequest 另请参见跨站点XMLHttpRequest

xmlhttprequest ordinarily does not support cross domain requests. xmlhttprequest通常不支持跨域请求。

You might want to use PHP cURL for some task like that 您可能要使用PHP cURL执行类似的任务

so you get your ajax to write to the php file that will 这样您就可以将ajax写入php文件,

then run your cURL post to the tumblr.com resource 然后将您的cURL发布运行到tumblr.com资源

A workaround to the XMLHttpRequest domain restriction is to create a proxy page at your domain that will receive the request and the make the remote call for you. XMLHttpRequest域限制的一种解决方法是在您的域中创建一个代理页面,该代理页面将接收请求并为您进行远程调用。

For example, if you were using ASP.NET your xmlhttp object could call AjaxProxy.aspx on your site, and that page could use a WebClient object to post the data to tumblr.com. 例如,如果您使用的是ASP.NET,则xmlhttp对象可以在您的站点上调用AjaxProxy.aspx,并且该页面可以使用WebClient对象将数据发布到tumblr.com。

Why are you using "POST" if you are appending the parameters to the URL? 如果将参数附加到URL,为什么要使用“ POST”? You could use "GET" instead. 您可以改用“ GET”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM