Https POST works with postman but preflight fails on chrome browser

I am new to ionic, angular …and unfortunately also to http.

I am trying to write a mobile app that fetches some data from server with https POST. I have a key and token for testing, and when I test the server with Chrome’s Postman the http post works like a charm and I get the response I need.

But when I’m trying the same with my test app running in Chrome browser it fails due to OPTIONS and PREFLIGHT.
I have installed “Allow-Control-Allow-Origin” plugin for Chrome so I don’t get CORS errors anymore.

This is what Postman sends to the server:

POST /somePath/GetResponseStatus HTTP/1.1
Host: some-server.com
mykey: my-cryptic-token
content-type: application/json
accept: application/json
Cache-Control: no-cache
Postman-Token: postmans-cryptic-token

This is how I do https post in ionic:

angular.module('myApp.user', [])
.factory('User', function($http){

var token = 'my-cryptic-token'		
return {
  GetResponse: function() {
    var config = {
      headers: {
        'mykey': token,
        'content-type': 'application/json',
        'accept': 'application/json' 
      }
    }
    return $http.post('https://some-server.com/somePath/GetResponseStatus', {}, config)
      .then(function(response) {
        return response.data
    })
  }
}
})

And this is what I get:

ionic.bundle.js:24977 OPTIONS some-server.com - some server
XMLHttpRequest cannot load some-server.com - some server. Response for preflight has invalid HTTP status code 401

I wonder if I am setting http headers correctly or not?
Clearly Postman works ok but how can I do the same trick with ionic?

I am using ionic CLI 1.7.14

Problem solved.
I use ‘content-type’: ‘text/plain’ instead.

1 Like