javascript - AngularJS 或 JQuery 解析错误格式 JSON (http get)

标签 javascript jquery ajax json angularjs

我正在尝试从远程服务器解析错误的 json 格式,如下所示:

//[
{},{} 
]

我在 AngularJS 中的代码:

$http.get('http://www.example.com/badjson')
    .success(function(data) {
        console.log(data);
});

但我收到错误:JSON.parse:JSON 数据第 1 行第 1 列出现意外字符。 我也无法使用 $.getJSON 在 Jquery 中使用它。有什么想法吗?

最佳答案

您需要使用 transformResponse 函数获取原始响应:

$http.get('http://www.example.com/badjson', { 
    transformResponse: function(d, h) { 
        return d; 
    }
})
.success(function(data) {
    conole.log(data);
});

关于javascript - AngularJS 或 JQuery 解析错误格式 JSON (http get),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29051815/

相关文章:

javascript - 按关键字数组过滤列表并按最佳匹配排序

javascript - ckeditor - 将文本区域插入数据库

jquery - 无法在 'readAsDataURL' : parameter 1 is not of type 'FileReader' 上执行 'Blob'

javascript - 以编程方式将 CSS 渐变添加为背景图像

javascript - 如何在没有 Fleegix.js 的情况下使用 timezone-js 加载时区文件?

jquery - 如何使用 jQuery 检测浏览器?

javascript - $(document).on ('focus' 或 $(document).bind ('input' ,不在动态添加的行上触发?

javascript - 如何在 jQuery 中仅显示一次警报

javascript - 在 Cpanel 上使用 Javascript AJAX POST 时出现 500 内部服务器错误

jquery - AJAX 动态调整此 HTML 的大小导致文本不可读。我怎么 "refresh"呢?