Javascript 运行ajax需要什么?向页面添加jquery是否会处理ajax调用?
我是ajax新手,在本地主机上运行xampp 我想在自己创建的页面中使用ajax与服务器通信 我想知道是否需要在head部分添加任何东西来使用ajax调用 在我的网页的标题部分,除了这个jquery文件外,没有包含任何javascript文件Javascript 运行ajax需要什么?向页面添加jquery是否会处理ajax调用?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我是ajax新手,在本地主机上运行xampp 我想在自己创建的页面中使用ajax与服务器通信 我想知道是否需要在head部分添加任何东西来使用ajax调用 在我的网页的标题部分,除了这个jquery文件外,没有包含任何javascript文件 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 但没有回应,我尝试了几次改变,但都没有成功。 我的代
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
但没有回应,我尝试了几次改变,但都没有成功。
我的代码中是否有错误,或者我没有包含ajax连接文件或其他内容?
如果您有任何帮助,我们将不胜感激。请参阅此处的代码
<script type="text/javascript" language="javascript">
$('#enter').click(function(event){
event.preventDefault();
$.ajax({
type: 'GET',
url: 'functions.php?id=enter_game',
//data: {id: 'enter_game'},
dataType: 'json',
cache: false,
success: function(result) {
if(result){
resultObj = eval (result);
alert( resultObj );
}else{
alert("error");
}
}
});
});
</script>
functions.php
<?php
echo "This is just to see if Ajax is working and it does !";
在客户端,包括jQuery就是发出ajax请求所需的全部操作。如果它不工作,则服务器的配置方式或php脚本都有问题。看看你的服务器是否抛出了任何错误,然后继续。感谢你的回复,我得到了问题并已经纠正了它,现在在functions.php上它显示了{“id”:{“name”:“fa”,“ghost”:“1”},“word”:“Apple”,“game”:“game start”}
但是在ajax响应中,我用警报(结果)检查了它代码>并在警报框中显示[object object]
。如何解码此对象以及如何在此对象中使用从服务器发送的值。先生,如果您有任何需要了解的参考资料,我们将不胜感激。请使用console.log
而不是alert。这将显示在web浏览器的控制台中。使用console,您将能够对对象进行解码。您可以通过进入开发者工具并单击console选项卡来获得console。您使用的是json
,因此向函数添加一个头。php
类似头('Content-Type:application/json')
现在您可以从响应中获取数据,如result.id.name
或result.word
<script type="text/javascript" language="javascript">
$('#enter').click(function(event){
event.preventDefault();
$.ajax({
type: 'GET',
url: 'functions.php?id=enter_game',
//data: {id: 'enter_game'},
dataType: 'json',
cache: false,
success: function(result) {
if(result){
resultObj = eval (result);
alert( resultObj );
}else{
alert("error");
}
}
});
});
</script>
<script type="text/javascript" language="javascript">
$('#enter').click(function(event){
event.preventDefault();
$.ajax({
type: 'GET',
url: 'functions.php?id=enter_game',
cache: false,
success: function(result) {
console.log(result);
}
});
});
</script>
<?php
echo "This is just to see if Ajax is working and it does !";