php part, converted to json format output
$list = json_encode($res1); echo $list;
jquery part
$.post("{:url('test')}",{ attr:attr, goods_id:$('input[name="goods_id"]').val() , spec_id:spec_id , sell_price:sell_price }, function(data,status){ data = eval('('+data+')');// Why do you need to add "(" ( "+data+" ) ");//" to eval here? console.log(data.msg); console.log(data.code); //alert(data['msg']); if(data.code=='1'){ layer.msg(data.msg, {icon: 1}); } if(data.code=='0'){ layer.msg(data.msg, {icon: 2}); } // var msg = data; //$('input[name="msg"]').val(data); //$(this).closest("tr").find('#status1').html(data); });
Why do you need to add "(" ( "+data+" ) ");//" to eval here?
The reason is: the problem of eval itself. Since json starts and ends with "{}", in JS, it will be
Treated as a statement block, so it must be forced to convert it to an expression.
The purpose of adding the parentheses is to force the eval function to force the expression inside the parentheses when processing JavaScript code
(expression) is converted to an object, not executed as a statement. For example, for
Like the literal {}, without the outer parentheses, eval recognizes the curly braces as the beginning of a JavaScript code block and
closing tag , then {} will be considered as executing an empty statement.