我收到错误:SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
我的代码:
<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"}
答案 0 :(得分:6)
问题是在你的JSON输出中你有3个对象:
{"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脚本以将所有对象推送到数组中,然后将该数组作为输出返回以获得如下结果:
[
{"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"}
]