简单的 Ajax(本地):responseText 为空
我尝试使用教程中的示例,但响应文本只是空的。如果我尝试使用“alert”,我会得到OK
,但是对于responseText,弹出窗口只是空的,里面什么也没有。这是为什么呢?
function start(){
var xhr = getXMLHttpRequest();
var sVar1 = encodeURIComponent("firstContent");
var sVar2 = encodeURIComponent("SecondContent");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
//alert("OK");
alert(xhr.responseText);
}
};
xhr.open("GET", "handlingData.php?variable1=" + sVar1 + "&variable2= " + sVar2, true);
xhr.send(null);
}
函数“start”由 onsubmit 调用:
form id="form_userlogin" onsubmit="start()"
PHP 页面:
<?php
header("Content-Type: text/plain");
$variable1 = (isset($_GET["variable1"])) ? $_GET["variable1"] : NULL;
$variable2 = (isset($_GET["variable2"])) ? $_GET["variable2"] : NULL;
if ($variable1 && $variable2) {
echo "OK";
} else {
echo "FAIL";
}
?>
我认为按照教程进行操作就可以了,但事实并非如此;p 如果您发现有什么问题,可以告诉我吗?
I've tried to use an example from a tutorial, but the response text is just empty. If I try with 'alert' I get OK
, but with the responseText, the popup is just empty, nothing in it. Why is this?
function start(){
var xhr = getXMLHttpRequest();
var sVar1 = encodeURIComponent("firstContent");
var sVar2 = encodeURIComponent("SecondContent");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
//alert("OK");
alert(xhr.responseText);
}
};
xhr.open("GET", "handlingData.php?variable1=" + sVar1 + "&variable2= " + sVar2, true);
xhr.send(null);
}
The function 'start' is called by the onsubmit:
form id="form_userlogin" onsubmit="start()"
And the PHP page:
<?php
header("Content-Type: text/plain");
$variable1 = (isset($_GET["variable1"])) ? $_GET["variable1"] : NULL;
$variable2 = (isset($_GET["variable2"])) ? $_GET["variable2"] : NULL;
if ($variable1 && $variable2) {
echo "OK";
} else {
echo "FAIL";
}
?>
I thought it would be ok to follow the tutorial, but it's not ;p
Can you please tell me if you see something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嘿伙计——
我可以建议你做一些事情让你的生活变得更轻松吗?
尝试 jQuery 的 ajax 库——
对你来说很简单......
Hey man --
can I suggest doing something to make your life a whole lot easier?
Try jQuery's ajax library -- so simple
in your case...
在许多浏览器中,Javascript 安全设置不允许通过
file:
协议加载的页面中包含的脚本进行 Ajax 调用。如果可能,请尝试从可以使用http:
协议的位置加载页面。 Apple 的开发人员网站,位于 http://developer.apple.com/internet/webcontent/xmlhttpreq。 html,还有一些其他考虑因素可能会导致您看到的结果。 (向下滚动到“安全问题”部分。)In many browsers, the Javascript security settings won't allow scripts contained in a page loaded via the
file:
protocol to make Ajax calls. If possible, try loading the page from a location where you can utilize thehttp:
protocol. Apple's developer site, at http://developer.apple.com/internet/webcontent/xmlhttpreq.html, has some other considerations that may cause the results you are seeing. (Scroll down to the "Security Issues" section.)