Uncaught SyntaxError: Unexpected token o on JSON.parse
本问题已经有最佳答案,请猛点这里访问。
我正在使用服务器API和脚本发送/接收JSON格式的数据,但是当我试图解析接收到的数据时,我得到了"未定义",这里是示例数据:
[{"id":"1","name":"Item 1","description":"","like_btn":"Burn in the hell,
bitch!","dislike_btn":"Forgive"},{"id":"2","name":"Item
2","description":"","like_btn":"Like
it","dislike_btn":"Happens worse"},{"id":"3","name":"Item
3","description":"","like_btn":"Explosed.","dislike_btn":"You're
really stupid!"},{"id":"4","name":"Item
4","description":"","like_btn":"You're in deep
shit!","dislike_btn":"Sometimes it happens."}]
这是JS代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | function DataTransmitter() { this.backendUrl = window.location.origin + '/backend/'; /** * Send GET request to the selected URI * @param {string} uri * @returns {json} */ this.get = function(uri) { $.ajax({ url: this.backendUrl + uri, dataType: 'json', success: function(data) { return JSON.parse(JSON.stringify(data)); } }); } /** * Return object with all post types * * @returns {json} */ this.getPostTypes = function() { return this.get('feed'); } /** * Get posts from feed with selected type (default: best) * * @param {string} type * @returns {json} */ this.getFeed = function(type) { var data; if(!type) type = 'best'; if(type == 'best' || type == 'fresh') { data = this.get('feed/' + type); }else{ data = this.get('feed/type/' + type) } return data; } } function AppApi() { this.transmitter = new DataTransmitter(); this.postTypes = null; this.getFeed = function(type) { var data = this.transmitter.getFeed(type); return data; } this.getPostTypes = function() { if(this.postTypes === null) { this.postTypes = this.transmitter.getPostTypes(); } return this.postTypes; } } api = new AppApi(); console.info(api.getPostTypes()); //<------ Here is problem now |
求你了,帮帮我!如何修复此错误?
数据类型:"json"尚未执行json.parse()。引发错误,因为JSON尝试分析此字符串[对象对象对象]