【发布时间】:2014-05-12 12:34:39
【问题描述】:
我正在尝试使用 ajax,我以前使用过它,从我以前的 html 文件中复制了代码并更改了发送的值,php 文件来处理代码,我正在使用 POST 方法。但是,php 脚本不会向 html 页面返回任何内容。我已经检查了所有我能做的,所以我现在在这里。 函数看(){ var x = document.getElementsByTagName('option')[document.getElementById("region").selectedIndex].value; var ajaxRequest; // 使 Ajax 成为可能的变量!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// modal section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById("accordion");
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
ajaxRequest.open("POST", "search.php", true);
ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
alert(x);
ajaxRequest.send("region=" + x);
}
php代码很简单
<?php
echo "The world is blue.";
?>
然而,它不会向所选的 div 返回任何内容。
【问题讨论】:
-
首先,我建议使用 jQuery 之类的框架,它使用 AJAX 让事情变得更容易。其次,你确定你从 PHP 中得到结果吗?
-
这就是问题所在,我没有从 php 得到任何结果。我有另一个可以正常工作的应用程序,但是,这个应用程序没有返回任何内容到 id 为“accordion”的 div。
-
@Michael You php 是原始的,必须工作。尝试为您的 javascript 代码使用 firebug。
标签: javascript php ajax