简体   繁体   English

错误语法错误:令牌异常

[英]ERROR Uncaught SyntaxError: Unexpected token <in JSON at position 0

I have the following JS script: 我有以下JS脚本:

jQuery(document).ready(function($) {
    $("#idSelect").change(function(event) {
        var valor = $(this).val();
        //alert(valor);
        $.post( "ajaxSerie.php", { valorInput: valor }, function( data ) {
            var retorno = JSON.parse(data);
            console.log(retorno);
            $("#pertence").val(retorno['pertence'])// aqui estou atribuindo um input qualquer o valor retornado do php, o input tera o valor de sala206
            $.each(retorno, function() {
                $('<option>').val(retorno['pertence']).text(retorno['pertence']).appendTo('#teste');
            });
        });
    });

}

in AJAX looks like this: 在AJAX中看起来像这样:

$idValor = $_POST['valorInput']; 
 $result = [
"pertence" => $idValor
];
echo json_encode($result);

when I do local it works perfectly, now when I step to the site on the server the following error: 当我在本地进行操作时,它运行完美,现在,当我进入服务器上的站点时,出现以下错误:

Uncaught SyntaxError: Unexpected token < in JSON at position 0 未捕获到的SyntaxError:JSON中位置0处的意外令牌<

how to solve this? 如何解决呢?

Your Ajax call might be returning HTML, which begins with a '<', the beginning of the opening HTML tag. 您的Ajax调用可能正在返回HTML,该HTML以“ <”(即打开的HTML标签的开头)开头。

In my experience, Ajax calls returning HTML is a sign that there has been an error in the back end. 以我的经验,Ajax调用返回HTML表示后端出现错误。 If you use your browser's developer tools, you'll be able to track your Ajax call and read its response. 如果使用浏览器的开发人员工具,则可以跟踪Ajax调用并读取其响应。

Take the time to read that HTML, because most of the time, the error message in the tag leaves a breadcrumb to find the real problem. 花时间阅读该HTML,因为在大多数情况下,标记中的错误消息会留下面包屑来查找真正的问题。

1st: $.post(url , {} , callback function(){} , 'json'); 1st: $.post(url , {} , callback function(){} , 'json'); you can add 'json' instead of using JSON.parse() 您可以添加'json'而不是使用JSON.parse()

2nd: if your server is running a PHP version older than 5.4 第二:如果您的服务器运行的PHP版本低于5.4

$result = [
"pertence" => $idValor
];

should be 应该

$result = array(
"pertence" => $idValor
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 控制台错误? - How to solve Uncaught SyntaxError: Unexpected token < in JSON at position 0 console error? 如何修复“Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0”错误 - How to fix “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” ERROR Redux 应用程序错误:未捕获(承诺中) SyntaxError:意外令牌 &lt; 在 position 0 处的 JSON - Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 反应错误 - Uncaught SyntaxError: Unexpected token o in JSON at position 1 - react error - Uncaught SyntaxError: Unexpected token o in JSON at position 1 未捕获到的SyntaxError:JSON中位置1处的意外令牌o - Uncaught SyntaxError: Unexpected token o in JSON at position 1 Uncaught SyntaxError: Unexpected token &#39; in JSON at position 2 - Uncaught SyntaxError: Unexpected token ' in JSON at position 2 Uncaught SyntaxError: Unexpected token : in JSON at position 362 - Uncaught SyntaxError: Unexpected token : in JSON at position 362 未捕获的语法错误:JSON 中的意外标记 D 位于位置 0 - Uncaught SyntaxError: Unexpected token D in JSON at position 0 未捕获到的SyntaxError:JSON中位置0处的意外令牌 - Uncaught SyntaxError: Unexpected token in JSON at position 0 未捕获到的SyntaxError:JSON中位置0处的意外令牌 - Uncaught SyntaxError: Unexpected token in JSON at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM