我跟着Bucky(新波士顿)关于Ajax的教程,第一课就被困住了
,这是我的问题:
Ajax不起作用。我在.js上设置了一些检查点警报,发现"readyState“从未命中4-我只收到3个警报:
我用Xampp在本地主机上运行,浏览器是Chrome和Firefox。
,这是代码:
index.html:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="foodstore.js"></script>
</head>
<body onload="process()">
<h3>The Chuff Bucket</h3>
Enter the food you would like to order:
<input type="text" id="userInput" />
<div id="underInput" />
</body>
</html>foodstore.php:
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = $_GET['food'];
$foodArray = array('tuna', 'bacon', 'beef', 'loaf', 'ham');
if(in_array($food, $foodArray))
echo 'We do have ' . $food . '!';
elseif($food=='')
echo 'Enter a food you idiot';
else
echo 'Sorry punk we dont sell no ' . $food . '!'
echo '</response>';
?>foodstore.js:
var xmlHttp = createXmlHttpRequestObject()
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if(!xmlHttp)
alert("cant create that object hoss!");
else
return xmlHttp;
}
function process(){
alert("1st checkpoint f(process) - readyState: " + xmlHttp.readyState);//
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
alert("2nd checkpoint f(process) - readyState: " + xmlHttp.readyState);//
food = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET", "foodstore.php?food=" + food, true);
xmlHttp.onreadystatechange = handleServerResponse();
xmlHttp.send(null);
}else{
setTimeout('process()', 1000);
}
}
function handleServerResponse(){
alert("1st checkpoint f(handleServerResponse) - readyState: " + xmlHttp.readyState);//
if(xmlHttp.readyState==4){
alert("2nd checkpoint f(handleServerResponse) - readyState: " + xmlHttp.readyState);//
if(xmlHttp.status==200){
xmlReponse = xmlHttp.responseXML;
xmlDocumentElement = xmlReponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("underInput").innerHTML = message;
//setTimeout('process()', 1000);
}else{
alert('Something went wrong!');
}
}
}任何帮助都很感激!
发布于 2014-11-06 20:27:50
在foodstore.js中,内部进程()替换以下一行:
xmlHttp.onreadystatechange = handleServerResponse();用这一行:
xmlHttp.onreadystatechange = handleServerResponse;这是因为您传递的是函数本身,而不是函数调用后的返回值。请参阅总是/
发布于 2014-12-17 17:16:56
这是巴基的AJAX教程。如果您被插入,这里是完整的工作代码:
index.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="foodstore.js"></script>
</head>
<body onload="process()">
<h3>The Chuff Bucker</h3>
Enter the food you would like to order:
<input type="text" id="userInput" />
<div id="underInput" />
</body>
</html>foodstore.php
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = $_GET['food'];
$foodArray = array('tuna','bacon','beef','ham');
if(in_array($food,$foodArray))
echo 'We do have '.$food.'!';
elseif ($food=='')
echo 'Enter a food you idiot';
else
echo 'Sorry punk we dont sell no '.$food.'!';
echo '</response>';
?>foodstore.js
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if(!xmlHttp)
alert("Cant create that object !")
else
return xmlHttp;
}
function process(){
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
food = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET", "foodstore.php?food="+food,true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}else{
setTimeout('process()',1000);//cekaj 1s pa probaj opet
}
}
function handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse = xmlHttp.responseXML; //izvlaci se xml sto smo dobili
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("underInput").innerHTML = message;
setTimeout('process()', 1000);
}else{
alert('Someting went wrong !');
}
}
}发布于 2014-09-01 03:48:52
以下是我如何处理这个问题的方法。
var userInput = $("#userInput").val();
$.ajax({
url: 'foodstore.php',
data: userInput,
method: 'GET',
success: function(response){
$("#underInput").html(response);
}
});就像你看到的那样干净多了!并做同样的事情:)
https://stackoverflow.com/questions/25598468
复制相似问题