Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX教程不起作用_Javascript_Php_Ajax - Fatal编程技术网

Javascript AJAX教程不起作用

Javascript AJAX教程不起作用,javascript,php,ajax,Javascript,Php,Ajax,我跟在后面,第一课就被卡住了=| 这是我的问题: Ajax不起作用。我在.js上设置了一些检查点警报,发现“readyState”从未达到4-我只收到3个警报: f(进程)第一个检查点-readyState:0 f(进程)第二个检查点-readyState:0 f(handleServerResponse)第一个检查点-readyState:1 我使用Xampp在localhost上运行,浏览器是Chrome和Firefox 代码如下: index.html: 感谢您的帮助 以下是我将如何处

我跟在后面,第一课就被卡住了=|

这是我的问题:

Ajax不起作用。我在.js上设置了一些检查点警报,发现“readyState”从未达到4-我只收到3个警报:

  • f(进程)第一个检查点-readyState:0
  • f(进程)第二个检查点-readyState:0
  • f(handleServerResponse)第一个检查点-readyState:1
我使用Xampp在localhost上运行,浏览器是Chrome和Firefox

代码如下:

index.html:
感谢您的帮助

以下是我将如何处理这个问题

var userInput = $("#userInput").val();
$.ajax({
   url: 'foodstore.php',
   data: userInput,
   method: 'GET',
   success: function(response){
       $("#underInput").html(response);
   }
});

就像你看到的那样,干净多了!并执行相同的操作:)

在foodstore.js的process()内部,替换此行:

xmlHttp.onreadystatechange = handleServerResponse();
xmlHttp.onreadystatechange = handleServerResponse;
这一行:

xmlHttp.onreadystatechange = handleServerResponse();
xmlHttp.onreadystatechange = handleServerResponse;

这是因为您传递的是函数本身,而不是调用函数后的返回值。请参见《Bucky的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>

当我遇到这个问题时,我改变了这一行

alert('Someting went wrong !');
为此:

alert('Someting went wrong ! readyState = ' + xmlHttp.readyState + ', Status = ' + xmlHttp.status);
我注意到我的状态始终为0,因此我在控制台中查看,发现以下错误:

XMLHttpRequest cannot load file: foodstore.php?food="+food
我发现了这个错误


基本上,出于安全原因,我的浏览器阻止了我的XMLHttpRequest请求。我将这些文件(+bug修复)上传到个人web服务器,AJAX工作正常!上面的链接中还列出了其他几种解决方法。

确保您正在使用的文件夹位于XAMPP的htdocs文件夹中

您想过改用jQuery吗?第一个问题是
xmlHttp.onreadystatechange=handleServerResponse()。。您需要为statechange分配一个函数引用,不要调用函数
xmlHttp.onreadystatechange=handleServerResponse@zsawaf是的,我做了,没有成功。事实上,我已经尝试了一切,本教程是我最后的选择。。。我已经使用Ajax好几天了。您正在尝试读取
onload
处理程序中
userInput
的值。。。在那里它是空的。。。也许你想使用点击处理程序?顺便说一句,试试类似的方法!在运行此代码之前,请确保已包含jquery:)如果要使用此代码运行,请确保将该代码包含在:$(document).ready(function(){…}中我得到一个[Uncaught TypeError:无法读取null的属性'documentElement'在这段代码中。错误指向foodstore.js中的第41行,xmlDocumentElement=xmlResponse.documentElement;帮助了很多人!请确保检查您是否将php XML文件的标题设置为text/XML而不是text/html,就像我忘记的那样。通过此更正,它将其设置为readyState 4和状态200,但由于某些原因ajax仍然不起作用ing…事实上,在我重新启动计时器并刷新页面几次后,它确实开始工作了。谢谢Joseph!
alert('Someting went wrong !');
alert('Someting went wrong ! readyState = ' + xmlHttp.readyState + ', Status = ' + xmlHttp.status);
XMLHttpRequest cannot load file: foodstore.php?food="+food