编辑:修复了 _ 拼写错误 (2x),添加了标题,仍然记录 100。
在我的 JavaScript 中单击一个按钮后,我将触发此函数(参数:100)
ajaxManager = new AjaxManager();
ajaxManager.requestHexContent(100);
function AjaxManager (){
this.requestHexContent = function(id){
$.ajax({
type : 'get',
url : 'simulator/hexFiller.php',
dataType : 'json',
data: {
gameid: id,
},
success : function(ret){
console.log(ret);
},
error : function(){
alert("error")
}
});
},
}
这是我的hexFiller.php
<?php
header('Content-Type: application/json');
$ret;
if (isset($_GET["gameid"])){
if ($_GET["gameid"] == 100){
$ret = 200;
}
else {
$ret = "error";
}
}
echo json_encode($ret);
?>
现在,我希望我的浏览器将“200”或“错误”记录到控制台。 相反,它将“100”记录到控制台。
有人可以向我解释一下我的想法中的根本错误吗?
最佳答案
正如评论中所讨论的,下面提到了工作代码: 我只用 $_GET 替换了 $GET。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
function AjaxManager() {
this.requestHexContent = function (id) {
$.ajax({
type: 'get',
url: 'simulator/hexFiller.php',
dataType: 'json',
data: {
gameid: id,
},
success: function (ret) {
console.log(ret);
},
error: function () {
alert("error")
}
});
}
}
ajaxManager = new AjaxManager();
ajaxManager.requestHexContent(100);
</script>
hexFiller.php
<?php
$ret;
if (isset($_GET["gameid"])) {
if ($_GET["gameid"] == 100) {
$ret = 200;
} else {
$ret = "error";
}
}
echo json_encode($ret);
?>
关于javascript - jQuery 基本 ajax GET + php 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33154246/