error SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data [duplicate]

拟墨画扇 提交于 2021-01-28 15:20:38

问题


I am getting Error : SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

my code:

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

And the JSON output:

{"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"}

回答1:


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"}

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"}
]


来源:https://stackoverflow.com/questions/21967960/error-syntaxerror-json-parse-unexpected-non-whitespace-character-after-json-da

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!