我使用的是React native 0.55.0。对于api我正在使用Axios,有时候我得到了无效的json
我得到一些时间json无效字符串添加无效字符串,例如" type_name" to" ty" type_name"。服务器端的Api工作正常。有一段时间它与json对象一起正常工作,没有得到什么问题
行动档案
export function getNotifications(params):Action {
return (dispatch) => {
dispatch({
type: SET_NOTIFICATIONS_LIST_REQUEST
})
return getNotificationsApi(params).then(resp => {
var data = resp.data
alert(typeof data) //it's return string
console.log(data)
dispatch(getNotificationsSuccess(data))
return resp.data
}).catch(err => {
})
}
}
我得到的数据
{"data":[{"id":583,"type":"itemcomment","type_id":10036,"datetime":"2018-05-15T06:48:39","status":1,"user_id":8,"type_name":"test new","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":581,"type":"itemcomment","type_id":10036,"datetime":"2018-05-15T06:48:05","status":1,"user_id":8,"type_name":"test new","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":578,"type":"itemcomment","type_id":10036,"datetime":"2018-05-15T06:42:50","status":1,"user_id":8,"type_name":"test new","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":449,"type":"editoutfits","type_id":5,"datetime":"2018-05-09T10:48:15","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":430,"type":"edititem","type_id":10034,"datetime":"2018-05-08T07:59:29","status":1,"user_id":8,"type_name":"Item3","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524568505IMG_0344.PNG"},{"image":"1525761327virtual_closet_mobile.jpg"}]},{"id":427,"type":"edititem","type_id":10034,"datetime":"2018-05-08T06:35:26","status":1,"user_id":8,"type_name":"Item3","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524568505IMG_0344.PNG"},{"image":"1525761327virtual_closet_mobile.jpg"}]},{"id":424,"type":"editoutfits","type_id":5,"datetime":"2018-05-08T05:44:08","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":421,"type":"editoutfits","type_id":5,"datetime":"2018-05-08T05:38:41","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":418,"type":"editoutfits","type_id":5,"datetime":"2018-05-08T05:38:29","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":355,"type":"editoutfits","type_id":5,"datetime":"2018-05-03T05:44:22","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":353,"type":"edititem","type_id":10025,"datetime":"2018-05-02T12:03:54","status":1,"user_id":8,"type_name":"Item1","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524201903virtual_closet_mobile.jpg"},{"image":"1524201903IMG_0190.PNG"}]},{"id":351,"type":"editoutfits","type_id":5,"datetime":"2018-05-02T05:48:26","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":283,"type":"addoutfits","type_id":8,"datetime":"2018-04-24T12:26:24","status":1,"user_id":6,"type_name":"Testttttt","user":{"username":"user2","id":"6","first_name":"sonam","last_name":"dogra"},"user_meta":{"value":"1524139232IMG_0186.PNG"},"oufitimg":"1524572784newlookImage.jpg"},{"id":250,"type":"additem","type_id":10034,"datetime":"2018-04-24T11:15:05","status":1,"user_id":8,"type_name":"Item3","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524568505IMG_0344.PNG"},{"image":"1525761327virtual_closet_mobile.jpg"}]},{"id":235,"type":"editoutfits","type_id":3,"datetime":"2018-04-24T10:18:03","status":1,"user_id":8,"type_name":"Outfit1","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525261115newlookImage.jpg"},{"id":232,"type":"addoutfits","type_id":5,"datetime":"2018-04-24T10:17:49","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":225,"type":"addoutfits","type_id":3,"datetime":"2018-04-24T10:10:26","status":1,"user_id":8,"type_name":"Outfit1","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525261115newlookImage.jpg"},{"id":207,"type":"likeitem","type_id":10026,"datetime":"2018-04-23T12:46:53","status":1,"user_id":8,"type_name":"Item2","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":206,"type":"itemcomment","type_id":10032,"datetime":"2018-04-23T12:35:29","status":1,"user_id":8,"type_name":"","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":190,"type":"additem","type_id":10026,"datetime":"2018-04-20T05:31:30","status":1,"user_id":8,"ty"type_name":"Item2","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524202290IMG_0125.PNG"},{"image":"1524202290virtual_closet_mobile.jpg"},{"image":"1524202290virtual_closet_mobile.jpg"}]},{"id":187,"type":"additem","type_id":10025,"datetime":"2018-04-20T05:25:03","status":1,"user_id":8,"type_name":"Item1","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524201903virtual_closet_mobile.jpg"},{"image":"1524201903IMG_0190.PNG"}]},{"id":60,"type":"acceptrequest","type_id":8,"datetime":"2018-04-19T17:23:46","status":1,"user_id":8,"type_name":"","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}}],"result":"success","image_path":"img\\/users"}
如果有人能,请提供帮助 这是内存问题
答案 0 :(得分:0)
我猜错误可能是由于你的函数覆盖了默认的transformResponse,它可以为你解析json。
请参阅https://github.com/axios/axios/issues/430#issuecomment-243481806帖子,了解Matt Zabriskie接受的答案。
另外,请不要在ES6语法中使用'var',但这是次要的。