【问题标题】:Alchemy sentiments text API json parse error炼金术情感文本 API json 解析错误
【发布时间】:2014-04-21 08:56:21
【问题描述】:

您好,我正在调用炼金术情感 API,如下所示:

function getAnalysis(sentence)
{
        $.ajax({
          url:alchemy.baseUrl,//http://access.alchemyapi.com/calls/text/TextGetTextSentiment`enter code here`
          type: 'POST',
          dataType:'jsonp',
          contentType:'json',
          data:{
              apikey:alchemy.acessKey,
              text:sentence,
              showSourceText:1,
              outputMode:'json'
              //outputMode:'xml'
          },
          context: this
          }).done(function(data){
              console.log('Sentiments Analysis sucessfull..');
              console.log(data);
          })
          .fail(function(jqXHR, textStatus, errorThrown) {

              console.log('Sentiments Analysis error:', textStatus, errorThrown);
          });

我的状态为 200 OK。但是解析错误:从ajax调用返回。我已经验证 JSON 是正确的。json 如下:

{
    "status": "OK",
    "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
    "url": "",
    "language": "english",
    "text": "sachin is a good batsman.",
    "docSentiment": {
        "type": "positive",
        "score": "0.50098"
    }
}

请帮帮我。

【问题讨论】:

    标签: json parse-error alchemyapi


    【解决方案1】:

    我已经通过修改 ajax 请求并添加如下回调解决了这个问题:

    function getAnalysis(sentence)
    {
            $.ajax({
              url: alchemy.baseUrl,
              type: 'POST',
              dataType:'jsonp',
              contentType:'json',
              jsonpCallback:'callback',
              data:{
                  apikey:alchemy.acessKey,
                  text:sentence,
                  showSourceText:1,
                  jsonp:'callback',
                  outputMode:'json'
    
                 },
              context: this
              }).done(function(data){
                  console.log('Sentiments Analysis sucessfull..');
                  console.log(data);
                  var text=data.text;
                  if(data.docSentiment.type==="negative")
                  {
                      displayNegetiveAnalysis(text);
                  }
                  else if(data.docSentiment.type==="positive"){
                      displayPositiveAnalysis(text);
                  }
               })
              .fail(function(jqXHR, textStatus, errorThrown) {
    
                  console.log('Sentiments Analysis error:', textStatus, errorThrown);
              });
    }
    /*
     * Function:Callback
     * Description:passing callback to URL Call
     * 
     */
    function callback(json){
        console.log(json);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 2016-06-21
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2015-01-21
      • 2014-10-12
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多