服务端返回一个数组,但是前段收到的不是一个数组

img


服务端返回一个数组

img


可是前段收到的却是这样的,求解


var arr = {};  //假如这是你服务器端返回的数据
var res = JSON.parse(arr.data);
console.log(res);  //这样就可以得到正确的数据了,望采纳

PHP生成的json后,前端需要使用ajax接收文件,content-type用json,post或get获取:
$.ajax({
accepts: {
mycustomtype: 'application/x-some-custom-type'
},

// Instructions for how to deserialize a mycustomtype
converters: {
'text mycustomtype': function(result) {
// Do Stuff
return result.**;
}
},

// Expect a mycustomtype back from server
dataType: 'mycustomtype'
});

加一个头试试


  header('content-type:application/json');

  echo json_encode(['code' => 0])