我似乎无法弄清楚如何使用ajax发布.我做了一个愚蠢的形式来尝试它,甚至在将它一直削减到只有两个值之后,仍然无法获得任何工作.我的
HTML是这样的:
Test this</title>First Name:
Last Name:
然后,到目前为止,我的外部JavaScript只是一个函数:
function postStuff(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "processForm.php";
var fn = document.getElementById("fname").value;
var ln = document.getElementById("lname").value;
var vars = "firstname="+fn+"&lastname="+ln;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
虽然我的PHP只是回复了这些东西:
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
echo $firstname ." - ". $lastname ."
";
?>
我在firebug或者chrome的工具中找不到任何错误.
任何人,我,我做错了什么?