【发布时间】:2011-09-17 03:51:08
【问题描述】:
祝大家有美好的一天。我有流动的问题。
我有 2 个域。在一个域上,我向另一个域发送 ajax 帖子并期待一些结果。问题是响应总是空的。如果我检查 net 选项卡,请求看起来没问题(发布数据很好),它没有收到任何错误,它结束(我在句柄响应函数上发出警报以检查响应是什么)。我尝试向随机域(例如 example.com)发送请求以查看是否有任何信息。反应是一样的……没有。
这是我使用的脚本:
function sendReqPost(url) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
//http_request.onreadystatechange = handleResponseAccept;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
//parameters is a global variable with the post data.
http_request.send(parameters);
}
我仔细检查了脚本中的所有内容...我还在请求的 php 页面中插入了 echo 以查看是否有任何内容。无论我做什么,响应都是空的。 附言在另一个域上,ajax 脚本运行良好。完全一样。
【问题讨论】:
-
http_request.responseText 在哪里?
-
这听起来像是您遇到了 XSS 限制。另外,请原谅我,因为我可能跑过去了,你知道你在异步模式下运行它正确吗?
-
考虑帮自己一个忙,并使用库(例如 jQuery)。整个代码将汇总到
$.post(url, parameters, function(){ /* on success here */ }); -
httpd_request.responseText 在句柄函数中。我没有在那里添加它,因为它只是 etc.rdyState == 4 { alert(http_request.responseText)}。
标签: php javascript ajax dns xmlhttprequest