如何在AngularJS中丢弃预检响应 [英] How to discard preflight response in AngularJS

查看:96
本文介绍了如何在AngularJS中丢弃预检响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向我的服务发送http.post请求时,我最初发送一个Option请求(根据Cors的要求)。

When sending a http.post request to my service I initially send an Option request (as required by Cors).

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

However I've realized that my OPTIONS (pre-flight) request is returning no response.data in its response, but my POST request is. This poses a problem as I need to access the response.data from the POST response...

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

Does Angular allow us to somehow discard the OPTIONS response in a http.post call?

$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回复:

Chrome responses:

推荐答案

你不能放弃预检OPTIONS请求。

You can't discard preflight OPTIONS request.

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

The purpose of this request is to ask the server for permissions to make the actual request. Your preflight response needs to acknowledge these headers in order for the actual request to work.

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

They are necessary when you're making requests across different origins.

这种飞行前请求是由某些浏览器作为安全措施制定的,以确保服务器信任所完成的请求。这意味着服务器理解在请求上发送的方法,来源和标题是安全的。

This pre-flight request is made by some browsers as a safety measure to ensure that the request being done is trusted by the server. Meaning the server understands that the method, origin and headers being sent on the request are safe to act upon.

在您的情况下,您应该在post api响应中获得响应

In your case you should get the response here in post api response

$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

//在您的控制器中,您可以调用该方法

//For more you can create external service and call api in that service
//in you controller you can call that method

这篇关于如何在AngularJS中丢弃预检响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆