从JSON响应中删除第一行 [英] Removing the first line from a JSON response
本文介绍了从JSON响应中删除第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我当前的代码是:
$.getJSON("https://www.domain.com/someapi/callback=?",
function(data){
$.each(data, function(i,item){
alert(item.x);
});
});
现在我遇到了错误,因为他们在json响应的顶部添加了一行.有办法摆脱那条线吗?
Right now I'm getting an error because they're adding a line to the top of my json response. is there a way to get rid of that line?
谢谢!
更新:
我也曾尝试做过这样的事情:
I have also tried doing something like this:
$.ajax({
url: "https://www.domain.com/someapi/callback=?",
type: "get",
success: function (data) {
alert(data)
}
});
但这是一个跨域调用,所以我得到了这个错误.
but it's a crossdomain call, so i get that error.
推荐答案
发出Ajax常规请求
$.ajax({
url : "https://www.domain.com/someapi/callback=?",
success : function(result){
// So here we get the result json with an error
// So lets say the response is something like this
/*
There was an error on line 35
{
json : true
}
*/
// So we remove everything before the first '{'
result = result.replace(/[^{]*/i,'');
//We parse the json
var data = JSON.parse(result);
// And continue like no error ever happened
$.each(data, function(i,item){
alert(item.x);
});
}
});
我希望这项工作. (必须从服务器启用跨域请求)
I hope this work. (a cross domain request must be enabled from the server)
这篇关于从JSON响应中删除第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文