且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript JSON.parse:意外字符错误

更新时间:2022-05-18 21:58:16

不要解析它:$.ajax为您解析了它.只需使用为您的success回调函数提供的参数,这就是已解析的数组.

Don't parse it : $.ajax parsed it for you. Just use the argument which is given to your success callback, this is the parsed array.

如果您的浏览器无法检测到它的JSON,请添加dataType参数.

If your browser can't detect it's JSON, add the dataType argument.

还请注意,您不必自己管理i计数器:一个由each传递:

Note also that you don't have to manage the i counter yourself : one is passed by each :

dataType: 'json',
success: function(sum){
    $('input.hj').each(function(i){
        if (sum[i] > 0) {
            $(this).val(sum[i]);
        }
    });
}