Search code examples
jsonparse-erroralchemyapi

Alchemy sentiments text API json parse error


Hi I am making the call for alchemy sentiments API as given below:

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);
          });

I am getting status 200 OK. But error in parsing : is returned from ajax call. I have validated JSON it is correct.The json is below:

{
    "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"
    }
}

Please help me.


Solution

  • I have solved the question just by modifying the ajax request and adding the callback as given below:

    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);
    }