简体   繁体   中英

Server responded with a status of 401 - No 'Access-Control-Allow-Origin' header - Angular Js - working fine with Jquery Ajax

Client side : AngularJS

Server / tech : Apache / Java Spring

First error was the No Access Control Allow Origin header not present. We fixed that by adding this code to the server side. The request is working fine with postman .

In Controller function http get method is returning error 401 (Unauthorized).

$http.get("our_local_url").then(function(data) {
                console.log("data:"+data);
            }, function(error) {
                console.log("error:"+error);
            });

A Test page with Jquery Ajax Call is working fine with the code

$.ajax({
        url: "our_local_url",
        type: "GET",
        success: function(result){
            console.log(result);
        },
        error: function(xhr,status,error){
            console.log(error);
        }
});

I've gone through several SO answers before asking this.

Add withCredentials: true in service call

try this below

$http.get("our_local_url", { withCredentials: true }).then{...};

There was some security handling in the spring-security which was blocking the call. After it's removed, it worked (No Access Control Allow Origin header added).

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