0

I was working with a Sentiment Analysis API which requires a curl request to get the JSON object.

How can this be done with AJAX?

This is the curl request:

$ curl -d "text=great" http://text-processing.com/api/sentiment/

This is what I have tried:

$.ajax("http://text-processing.com/api/sentiment/", {
    type: "POST",
    data: JSON.stringify({ text: "Hello world!" }),
    contentType: "application/json",
    }).done(function(data){
        alert('done'); 
    }).fail(function(xhr, status, error){

});
  • 1
    did you try anything? – madalinivascu Jun 12 '18 at 11:43
  • $.ajax("http://text-processing.com/api/sentiment/", { type: "POST", data: JSON.stringify({ text: "Hello world!" }), contentType: "application/json", }).done(function(data){ alert('done'); }).fail(function(xhr, status, error){ }); – Prashant Upadhyay Jun 12 '18 at 11:46
  • any console errors when you use that code? – madalinivascu Jun 12 '18 at 11:47
  • Failed to load resource: the server responded with a status of 405 (METHOD NOT ALLOWED) popup.html:1 Failed to load http://text-processing.com/api/sentiment/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 405. – Prashant Upadhyay Jun 12 '18 at 11:49
  • see https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present – madalinivascu Jun 12 '18 at 11:50
  • Please, put the error message in your question rather than in commentaries. – Vincent Jun 12 '18 at 12:44
  • this means the remote API doesn't support CORS requests, so you cannot use AJAX to access this API. You'll have to write a server-side script to access the API, and then load the response into your web page. – ADyson Jun 12 '18 at 14:33

0 Answers0