错误语法错误:JSON.parse:JSON数据后出现意外的非空白字符 [英] error SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

查看:104
本文介绍了错误语法错误:JSON.parse:JSON数据后出现意外的非空白字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息: SyntaxError:JSON.parse:JSON数据后出现意外的非空白字符

我的代码:

<script>
    $(document).ready(function () {
        $('.edit1').on('change', function () {
                arr = $(this).attr('class').split(" ");
                var clientid = document.getElementById("client").value;
                account_id = document.getElementById("account_id").value;
                $(this).parent().next().find('input:checkbox').attr("checked", true);
                $.ajax({
                    type: "POST",
                    url: "clientnetworkpricelist/routestatusupdate.php",
                    data: "value=" + $(this).val() + "&rowid=" + arr[2] + "&field=" + arr[1] + "&clientid=" + clientid + "&account_id=" + account_id,
                    success: function (result) {
                        data = jQuery.parseJSON(result); //added line

                        var obj = data;

                        $('#CPH_GridView1_Status' + arr[2]).empty();
                        $('#CPH_GridView1_Status' + arr[2]).append(data.status);

                        $('.ajax').html($(this).val());
                        $('.ajax').removeClass('ajax');
                    }
                });
            }
        );
    });
</script>

和JSON输出:

{"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}{"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}{"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}

推荐答案

问题是,在JSON输出中,您有3个对象:

The problem is that in your JSON output you've got 3 objects:

{"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}
{"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}
{"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}

彼此相邻放置.尝试修改您的PHP脚本,以将所有对象推送到数组中,然后将该数组作为输出返回,以得到如下结果:

which are just placed inline next to each other. Try to modify your PHP script to push all that objects into array and then return that array as output to get result like:

[
  {"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"},
  {"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"},
  {"status":"<img  src=\"image\/Equalf.png\" \/>","seleniumrouteupdate":"1","routeupdate":"100"}
]

这篇关于错误语法错误:JSON.parse:JSON数据后出现意外的非空白字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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