错误未捕获的语法错误:JSON位置0内的意外令牌< [英] ERROR Uncaught SyntaxError: Unexpected token <in JSON at position 0

查看:85
本文介绍了错误未捕获的语法错误:JSON位置0内的意外令牌<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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');
            });
        });
    });

}

AJAX看起来像这样:

in AJAX looks like this:

$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:


未捕获到的SyntaxError:意外令牌< JSON中位置0

Uncaught SyntaxError: Unexpected token < in JSON at position 0

如何解决此问题?

推荐答案

您的Ajax调用可能正在返回HTML,该HTML以'<'开头(即打开的HTML标签的开头)。

Your Ajax call might be returning HTML, which begins with a '<', the beginning of the opening HTML tag.

在我的经验,Ajax调用返回HTML表示后端出现错误。如果您使用浏览器的开发人员工具,则可以跟踪Ajax调用并读取其响应。

In my experience, Ajax calls returning HTML is a sign that there has been an error in the back end. If you use your browser's developer tools, you'll be able to track your Ajax call and read its response.

花时间阅读HTML,因为大多数时间,标记中的错误消息会留下一个面包屑来查找真正的问题。

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.

这篇关于错误未捕获的语法错误:JSON位置0内的意外令牌&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆