首页 \ 问答 \ 无法解析来自jquery Ajax Call的JSON响应(Unable to parse JSON response from jquery Ajax Call)

无法解析来自jquery Ajax Call的JSON响应(Unable to parse JSON response from jquery Ajax Call)

我对jquery / ajax比较陌生,而且我很难从一个非常简单的JSON ajax调用中解析结果。 这是我的代码 。 此示例中有2个测试URL:注释的URL按预期工作,但未注释的URL不符合(根据jslint,它们都是格式正确的JSON)。 知道为什么会抛出一个解析错误,另一个不会(它们都是第三方域)?

先谢谢你!

function getNews2() {

$.ajax({

    //url: "http://api.ihackernews.com/page?format=jsonp",
    url: "http://recs.coremetrics.com/iorequest/restapi?cm_cid=90232642&cm_zoneid=Mobile&cm_targetid=FULO-0101",
    dataType: "jsonp",
    success: function(data, textStatus, xhr) {
        alert("SUCCESS recsStatus=" + textStatus);
        alert(JSON.stringify(data));
    },
    error: function(data, textStatus, errorThrown) {
        alert("FAILURE recsStatus=" + textStatus);
        alert(JSON.stringify(data));
    }
});
}

getNews2();

I'm relatively new to jquery/ajax, and I'm having trouble parsing the results from a very simple JSON ajax call. Here's my code. There are 2 test URLs in this example: the commented URL works as expected, but the uncommented URL does not (they are both properly formatted JSON, according to jslint). Any idea why one would throw a parse error, and the other would not (they are both 3rd party domains)?

Thank you in advance!

function getNews2() {

$.ajax({

    //url: "http://api.ihackernews.com/page?format=jsonp",
    url: "http://recs.coremetrics.com/iorequest/restapi?cm_cid=90232642&cm_zoneid=Mobile&cm_targetid=FULO-0101",
    dataType: "jsonp",
    success: function(data, textStatus, xhr) {
        alert("SUCCESS recsStatus=" + textStatus);
        alert(JSON.stringify(data));
    },
    error: function(data, textStatus, errorThrown) {
        alert("FAILURE recsStatus=" + textStatus);
        alert(JSON.stringify(data));
    }
});
}

getNews2();

原文:https://stackoverflow.com/questions/8855048
更新时间:2023-09-13 16:09

最满意答案

测试表和数据:

DECLARE @t table(id int identity(1,1), col1 varchar(max))
insert @t values
('item'+char(1)+ 'value1'+char(1)+'value4'+char(1)+char(0)+ 'item'+char(1)+'value1'+char(1)+ 'value2'+char(1)+char(0)),
('item'+char(1)+ 'value1'+char(1)+'value2'+char(1)+char(0)+ 'item2'+char(1)+'value1'+char(1)+ 'value2'+char(1)+char(0))

查询:

;WITH SplitByRow as
(
        SELECT id, t.c.value('.', 'VARCHAR(2000)') colx
        FROM (
            SELECT id, x = CAST('<t>' + 
                REPLACE(REPLACE(col1, char(1), '!new row!'), char(0), '</t><t>') + '</t>' AS XML)
        FROM @t
        ) a
        CROSS APPLY x.nodes('/t') t(c)
), SplitByColumn as
(
  SELECT 
    id,
    (row_number() over (order by (select 1))+2)/3 rowx, 
    (row_number() over (order by (select 1))+2)%3 colx,
    t.c.value('.', 'VARCHAR(2000)') value
    FROM 
    (
        SELECT id, x = CAST('<t>' + 
            REPLACE(colx, '!new row!', '</t><t>') + '</t>' AS XML)
        FROM SplitByRow
    ) a
    CROSS APPLY x.nodes('/t') t(c)
    WHERE t.c.value('.', 'VARCHAR(2000)') <> ''
)
SELECT 
  id, [0] col1,[1] col2,[2] col3
FROM 
  SplitByColumn 
INTO 
  #temp
PIVOT
    (min([value]) FOR colx
    in([0],[1],[2])  
)AS p

结果:

SELECT * FROM #temp

id  col1    col2    col3
1   item    value1  value4
1   item    value1  value2
2   item    value1  value2
2   item2   value1  value2

Test table and data:

DECLARE @t table(id int identity(1,1), col1 varchar(max))
insert @t values
('item'+char(1)+ 'value1'+char(1)+'value4'+char(1)+char(0)+ 'item'+char(1)+'value1'+char(1)+ 'value2'+char(1)+char(0)),
('item'+char(1)+ 'value1'+char(1)+'value2'+char(1)+char(0)+ 'item2'+char(1)+'value1'+char(1)+ 'value2'+char(1)+char(0))

Query:

;WITH SplitByRow as
(
        SELECT id, t.c.value('.', 'VARCHAR(2000)') colx
        FROM (
            SELECT id, x = CAST('<t>' + 
                REPLACE(REPLACE(col1, char(1), '!new row!'), char(0), '</t><t>') + '</t>' AS XML)
        FROM @t
        ) a
        CROSS APPLY x.nodes('/t') t(c)
), SplitByColumn as
(
  SELECT 
    id,
    (row_number() over (order by (select 1))+2)/3 rowx, 
    (row_number() over (order by (select 1))+2)%3 colx,
    t.c.value('.', 'VARCHAR(2000)') value
    FROM 
    (
        SELECT id, x = CAST('<t>' + 
            REPLACE(colx, '!new row!', '</t><t>') + '</t>' AS XML)
        FROM SplitByRow
    ) a
    CROSS APPLY x.nodes('/t') t(c)
    WHERE t.c.value('.', 'VARCHAR(2000)') <> ''
)
SELECT 
  id, [0] col1,[1] col2,[2] col3
FROM 
  SplitByColumn 
INTO 
  #temp
PIVOT
    (min([value]) FOR colx
    in([0],[1],[2])  
)AS p

Result:

SELECT * FROM #temp

id  col1    col2    col3
1   item    value1  value4
1   item    value1  value2
2   item    value1  value2
2   item2   value1  value2

相关问答

更多