我在使用 ajax 调用 php 文件时遇到语法错误。
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
Uncaught SyntaxError: Unexpected token F
change_produkt
函数通过复选框字段中的 onclick 事件调用。
该函数的输出如下:
现在,我调用第二个函数 fill_optionen
并将数组传递给该函数。它正在为每个对象执行 ajax 调用。 (本例中为 6 次)
Javascript:
function fill_optionen(optionen) {
console.log("fill_optionen called.."); // Debug
var text = "";
$j.each(JSON.parse(optionen), function (index, value) {
jQuery.ajax({
url: 'include/mutation_helper.php',
data: {func: "render_opt", option: value},
type: 'post',
success: function(output) {
console.log(JSON.parse(output)); // Debug
}
});
});
}
function change_produkt() {
console.log("change_produkt called.."); // Debug
var id_produkt = $j("#produkt").val();
console.log("DEBUG -- id_produkt:"+id_produkt);
jQuery.ajax({
url: 'include/mutation_helper.php',
data: {func: "get_opts", produkt: id_produkt},
type: 'post',
success: function(output) {
console.log(JSON.parse(output)); // Debug
fill_optionen(output);
}
});
}
PHP:
function render_opt() {
if(!isset($_POST['option'])) {
echo json_encode("error");
exit;
}
$opt = $_POST['option'];
$render = render($opt, $_SESSION['mutation']);
echo json_encode("hello");
}
一旦我删除调用 render
函数的行,它就会工作。但是为什么会出错呢?我什至没有打印出那个 $render
变量。
(render
函数只返回字符串中的 html 代码。)
最佳答案
您的 render
函数中似乎有一个 PHP 错误。然后 PHP 打印出它的错误,随后导致 JavaScript 错误。因为 JSON 被排除在 ajax 请求的返回输出之外,并且您得到了一个包含 PHP 错误消息的字符串。请查看你的浏览器调试器在ajax请求原始网络数据下,真正返回了什么。
关于javascript - 从 ajax 调用函数时获取 "Syntax Error: Unexpected Token",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28148818/