简体   繁体   中英

$http.get() with Basic Auth is failing at OPTIONS call in AngularJS, IONIC

I am making a call from ionic app with following code:-

 var req={ method: "GET", url: "<Some URL>", headers:{ "Authorization": "Basic YjEyMDMwOmJjb25lQDEyMw==" } } $http(req).then(function success(){ alert("Yesss"); },function failure(a,b,c){ alert("nahhh"); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 

Below image shows failure in OPTIONS call.

Failing call in console

Whereas same call works fine from SAPUI5 which takes care of CORS internally with oModel.Read() operation. Below is the successful call screenshot:-

Passing call in console

What am i doing wrong? or is it even possible to make CORS based call from AngularJS app?

edit1:

I just noticed, if i give header- Authorization, rest of the headers are ignored, and accept goes with text/plain... etc But if authorization header is not there, it takes Accept and other params, but since my service is Basic auth based, it would need this header, now how do i crack this?

edit2: I tried same thing in $.get ajax call, and noticed, if i pass "Authorization" header or any other random header like "doijwoid" no other headers are shown in request header calls, but when i omit this header other headers like "Accept" works as written in code.

 $.get({url:"<SRV URL>",headers:{"Accept":"application/json"}}, function(data, status){ alert("Data: " + data + "\\nStatus: " + status); }); 

[Ajax call without authorization header][3]

[Call with Authorization header and Accept as well, but accept header not taken][4]

您是否已在服务器上签入CORS?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM