1

我正在尝试使用Viralheat 的 API使用 jQuery 进行情绪分析。

1.当我在浏览器的地址栏中输入这个,就可以了:

https://app.viralheat.com/social/api/sentiment?api_key=1234&text=Schindler%27s%20List

它显示:

{"status":403,"error":"API Key not registered"}

2.使用这些代码行,网页显示错误,我想它运行到同源策略:

    $.ajax({
        type: 'get',
        url: "https://app.viralheat.com/social/api/sentiment?api_key=1234&text=Schindler's List",
        success: function(data){alert(data);},
        error: function(){alert("error");}
    });

3.我callback=?在url中加a,dataType: 'json',使用firebug,可以看到和场景1一样的结果{"status":403,"error":"API Key not registered"},但是网页还是报错,出现错误:SyntaxError: invalid label

    $.ajax({
        type: 'get',
        dataType: 'json',
        url: "https://app.viralheat.com/social/api/sentiment?api_key=1234&text=Schindler's List&callback=?",
        success: function(data){alert(data);},
        error: function(){alert("error");}
    });

我该如何解决这个问题?

4

1 回答 1

4

仅仅添加callback=whatever到 JSON API 请求不会神奇地将结果转换为 JSONP,除非服务实际上支持这样的 JSONP 选项。

您使用的 Viralheat API 似乎不支持 JSONP。该文档没有说明任何类型的 JSONP 选项。它提到的唯一选项是API_KEYTEXTPARAMETERS。

正如您所发现的,添加callback=anything不会将输出格式从 JSON 更改为 JSONP。如果您尝试使用添加参数的 API 调用callback=foobar,它仍会生成普通 JSON:

{
    "status": 403,
    "error": "API Key not registered"
}

如果此服务不支持 JSONP 或CORS,您将不得不使用服务器上的代码而不是客户端浏览器中的 JavaScript 来访问该服务。

于 2013-08-11T04:18:37.593 回答