简体   繁体   English

Angular2 Http.get发送两个请求

[英]Angular2 Http.get Send two request

i have simple authentication service im using jwt token and after if ill need user detail for check authorization header and send request api end point but angular 2 request send first request Header empty key but second correct header let me explain 我有使用jwt令牌的简单身份验证服务,如果生病后需要用户详细信息检查授权头并发送请求api端点,但是角度2请求发送第一个请求头空键,但是第二个正确的头让我解释一下

Network Operations http://pasteboard.co/c3wmFZvtJ.jpg 网络运营http://pasteboard.co/c3wmFZvtJ.jpg

Wrong Header http://pasteboard.co/c3wDdFxMy.jpg 标头错误http://pasteboard.co/c3wDdFxMy.jpg

Correct Header http://pasteboard.co/1cKy9EHDy.jpg 正确的标题http://pasteboard.co/1cKy9EHDy.jpg

My Http.Get Function 我的Http.Get函数

getUsers(): Observable<User[]> {
    // Authorization Tokeni Ayarlanıyor
    let headers = new Headers({ 'Authorization': this.authenticationService.token });
    let options = new RequestOptions({ headers: headers });
    // Kullanıcı Headeri Gönderiliyor
    return this.http.get('http://localhost/Hesap/Detay', options)
        .map((response: Response) => 
            response.json().detay
     ); 
}

i Call this Function Here 我在这里叫这个功能

ngOnInit() {
    this.userService.getUsers()
        .subscribe(users => {
            this.users = users;
        });
}

Php Side php侧

public function Detay(){
    echo $this->headers["authorization"];
    if(!isset($this->headers["authorization"]) || empty($this->headers["authorization"])){
        echo json_encode(array("Hata" => "Header Yok"));
    }else{
        $token = explode(" ", $this->headers["authorization"]);
        $user = JWT::decode(trim($token[0],'"'));
        $this->load->model("auth_model");
        if($this->auth_model->checkUser($user->id, $user->KullaniciAdi) !== false)
            {
                $this->load->model("user_model");
                $detay = $this->user_model->get($user->id, $user->KullaniciAdi);
                echo json_encode(
                    array( 
                            "detay"=> $detay
                    )
                );
            }
        }
}

Second Header can echo Token http://pasteboard.co/c3C6ed2k7.jpg 第二个标头可以回显令牌http://pasteboard.co/c3C6ed2k7.jpg

And Now THİS PROBLEM ONLY DEVELOPMENT MODE if i build prod project send one request 现在,如果我构建产品项目,则仅此问题开发模式发送一个请求

It looks like the first request is an OPTIONS request needed because of CORS . 似乎第一个请求是由于CORS所需的OPTIONS请求。

If that is the case, there is nothing wrong with that. 如果是这样的话,那没有什么错。 The browser needs to make a prior request to check if you can call the api and if the Headers you want are allowed. 浏览器需要事先发出请求,以检查是否可以调用api以及是否允许使用所需的标头。

The header Access-Control-Request-Headers checks if you can send the authorization header in your request. 标头Access-Control-Request-Headers检查是否可以在请求中发送authorization标头。

The header Access-Control-Request-Method checks if you can send a GET request. 标头Access-Control-Request-Method检查您是否可以发送GET请求。

The preflight request only happens if the domain of the client is different from the domain of the api. 仅当客户端的域与api的域不同时,才执行预检请求。

Take a look at this link . 看一下这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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