我正在尝试使用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 返回任何内容。
您可能没有发布足够的信息让我们回答您的问题。但是,我承认您可能不确定首先需要问什么问题。
首先,我必须同意@cgf.jQuery或类似的将使这更容易。
但是,让我们专注于手头的问题。 我们需要知道幕后发生了什么。
最好的方法是通过开发人员工具栏,如Firebug。Chrome内置了一个。
因此,加载铬并点击 F12。这应该会在窗口底部显示一个非常繁忙的窗格。选择"网络"选项卡,然后请求页面/触发JavaScript。您在网络选项卡中获得什么输出?主要是,在状态下,您会得到 200、500(服务器错误)还是 404(未找到)?用您看到/得到的内容更新您的上述问题。
我希望这应该为你指明正确的方向。