gpt4 book ai didi

javascript - 如何在 AngularJS 中丢弃预检响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:25:47 26 4
gpt4 key购买 nike

在向我的服务发送 http.post 请求时,我首先发送了一个 Option 请求(根据 Cors 的要求)。

但是我意识到我的OPTIONS(飞行前)请求在其响应中没有返回response.data,但是我的POST 要求是。这造成了一个问题,因为我需要从 POST 响应中访问 response.data...

Angular 是否允许我们以某种方式丢弃 http.post 调用中的 OPTIONS 响应?

$http.post("http://api.worksiteclouddev.com/RestfulAPI/api/vw_PeopleListSiteAccess_Update/Get/", JSON.stringify(vm.content))
.then(function (response) {
//success

vm.person = {
"firstname": response.data.Firstname,
"surname": response.data.surname
};
console.log(response.data);
},
function (error) {
//failure
console.log("Something went wrong..." + error.status);
})
.finally(function () {
vm.ptsnumber = "";
});

Chrome 响应: enter image description here

最佳答案

您不能放弃预检 OPTIONS 请求。

此请求的目的是向服务器请求发出实际请求的权限。您的预检响应需要确认这些 header 才能使实际请求生效。

当您跨不同来源发出请求时,它们是必需的。

此飞行前请求是由某些浏览器作为安全措施发出的,以确保所完成的请求受到服务器的信任。这意味着服务器了解在请求中发送的方法、来源和 header 可以安全地采取行动。

在你的情况下,你应该在 post api 响应中得到响应

$http.post(
"http://api.worksiteclouddev.com/RestfulAPI/api/vw_PeopleListSiteAccess_Update/Get/", JSON.stringify(vm.content)
).then(function (response) {
console.log(response.data);
//You should get your response here
},
function (error) {
console.log("Something went wrong..." + error.status);
});

//更多你可以创建外部服务并在该服务中调用api
//在你的 Controller 中你可以调用那个方法

关于javascript - 如何在 AngularJS 中丢弃预检响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43997953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com