0

I don't sure if it's related to ionic or the fact that it runs currently on localhost, but request returns with Request Method: OPTIONS, 400 instead of POST and returns 400.

what can be the problem?

this is the API I use: https://languagetool.org/http-api/swagger-ui/#!/default/post_check

and this is my code :

factory.checkText = function(text) {
  var language = "en-US";
  var reqUrl = "https://languagetool.org/api/v2/check";
  var data = {
    language: language,
    text: text,
    enabledOnly: false
  };
  var deferred = $q.defer();
  var request = {
    method: 'POST',
    url: reqUrl,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    headers: {
      'Content-Type': 'application/json',
      'Cache-Control': 'no-cache',
      'X-Requested-With': 'XMLHttpRequest',
      'Access-Control-Allow-Origin': '*'
    },
    data: JSON.stringify(data),
  };

  $http(request).success(function(data) {
    deferred.resolve(data);
  }).error(function(error) {
    deferred.reject(error);
  });
  return deferred.promise;
}

If I use

params: data 

instead of

data: JSON.stringify(data)

, I still get OPTION but with 200. but error event is fired, so I can't read the response.

  • what does it mean? I can't use it from other domain? – user2837319 May 01 '17 at 06:38
  • Regarding the duplicate, note in the API documentation that the parameter types are `formData`. You are attempting to send JSON data. I find the [second-highest voted answer](http://stackoverflow.com/a/30970229/283366) to be the best one – Phil May 01 '17 at 06:40
  • 1
    Also, you have implemented the [deferred anti-pattern](http://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern) – Phil May 01 '17 at 06:43
  • @Phil many thanks!!! this is what I needed – user2837319 May 01 '17 at 10:01

0 Answers0