本文介绍了飞行前响应不成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有angular的Web应用程序,它向Azure API发送请求。 API受角度保护。当我在浏览器中调用url时,我会被重定向到microsoft登录页面。登录后我回到了API。



现在,我想从我的角度应用程序向API发送请求:

  const auth = btoa([我的用户名]:[我的密码]); 
headers = {授权:基本+ auth};

$ http.get(http:// [webapp name] .azurewebsites.net / api / contacts,{headers:headers})。then(function(response){
console.log(!!! LoggedIn !!!);
});

我在[azure portal]中将[webapp name] .azurewebsites.net添加到我的CORS中。
当我执行此操作时,我收到错误:

azurewebsites.net/api/contacts. Preflight response is not successful

Any idea how to fix it?

UPDATE

I tried it again with this code:

const auth = btoa("[my username]:[my password]");
var config = {headers:  {
              'Authorization': 'Basic ' + auth,
              "Origin": "http://[webapp name].azurewebsites.net/api/contacts",
              "Access-Control-Request-Method": "GET",
              "Access-Control-Request-Headers": "X-Custom-Header"
              }
};

$http.get("http://[webapp name].azurewebsites.net/api/contacts", config).then(function (response) {
    console.log("!!!LoggedIn!!!");
});

Now I'm getting these errors:

When I delete these "unsafe headers" the last error message is still there.

Why does Origin is null automatically?

解决方案

You should add the address of your requesting site URL in CORS.

You can confirm you have added correct origin by inspecting network tab in developer's console.

First select the request with method OPTIONS

Then verify the Access-Control-Allow-Origin is the same with your Origin

这篇关于飞行前响应不成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 04:37
查看更多