Gourav
Gourav

Reputation: 115

ionic calling restApi with header

Everyone

I am new with ionic framework. When calling rest API with header field and passing token with header field. API always call twice one preflight request and second POST.

In preflight request I am getting status code 204 and on POST it is working fine with status code 200. but when calling rest API without token it is calling only once

var head = { 'X-Auth-Token' : window.localStorage.getItem('token') };

this.getlist = function (json) { return post('restapi url',json); }; 

function post(targetUrl, json) { 
  var deferred = $q.defer(); 
  $http({ url: targetUrl, method: "POST", data: json, headers: head }).success(function (data, status, headers, config) { deferred.resolve(data); })     
  return deferred.promise; 
}

Upvotes: 1

Views: 171

Answers (2)

anshul goyal
anshul goyal

Reputation: 26

https://www.html5rocks.com/static/images/cors_server_flowchart.png

please see this image. this image have complete flow for preflight request

Upvotes: 1

Chetan S. Choudhary
Chetan S. Choudhary

Reputation: 310

If Request should not have any custom header parameter, If request header contains any custom header then browser will make pre-flight request, you cant avoid it

Upvotes: 1

Related Questions