【发布时间】:2016-02-22 14:16:49
【问题描述】:
我对使用 jQuery 的 AJAX 结果有疑问。
我已经定义了这些函数:
<script>
function hello(callback, funct, val) {
var ret = 0;
console.log(val);
$.ajax({
type: 'GET',
dataType: 'json',
url: 'SGWEB/header.php',
data: {
'funct': funct,
'val': val
}
}).done(function (data) {
// you may safely use results here
console.log(data);
callback(data);
});
};
function change() {
hello(function (ret) {
console.log(ret);
$("#b1").text(ret);
}, "hello", 1);
};
change();
</script>
SGWEB/header.php:
extract($_GET);
$validFunctions = array("readPin","hello");
if(in_array($funct, $validFunctions)) $funct();
// functions
// ....
function hello($val) {
if ($val == 1) {
echo "1";
} else
echo "2";
}
我遇到的问题是 AJAX 仅传递数据 {'funct': funct} 中的第一个参数并且它正在工作,但 val 被完全忽略(它总是回显“2”)。
我该如何解决这个问题?谢谢
【问题讨论】:
-
控制台有什么显示吗?
-
您将 function 传递给
hello(),当您将其与1进行比较时,这是错误的。这就是它总是转到else分支的原因。跨度> -
@A1rPun javascript hello 函数(代码顶部)需要一个回调函数作为它的第一个参数......代码底部的 hello 函数是一个 PHP 函数(我认为)
-
我希望看到更多
SGWEB/header.php以了解它如何处理传入查询以最终调用 hello 函数......当然标记为 PHP 函数的代码并不是那个 header.php -
您需要添加正确的 PHP。我没有看到
SGWEB/header.php的内容,也没有看到您是如何在 PHP 中获取 GET 数据的。
标签: javascript php jquery ajax