首页 > 解决方案 > 使用 HTTP Ionic 的标头是不同的标头 PHP

问题描述

我正在尝试使用@angular/http在 Ionic 中发送请求

在服务器旁(nodejs)我打印req.header

当请求通过 PHP/Postman/curl/Ionic Server Proxy 添加时,标头为:

{
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "access_token": "foo",
        "cache-control": "no-cache",
        "connection": "keep-alive",
        "content-length": "0",
        "content-type": "application/json",
        "host": "localhost:8081",
        "login": "11111111111",
        "postman-token": "72260d7c-6bd8-449a-95a7-290bf44bc61d",
        "senha": "1234",
        "user-agent": "PostmanRuntime/7.3.0"
    }

但是当我使用 Ionic Http 请求发送请求时,标头是:

{
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "accept-encoding": "gzip, deflate",
        "accept-language": "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
        "access-control-request-headers": "access_token,content-type,login,senha",
        "access-control-request-method": "POST",
        "cache-control": "no-cache",
        "connection": "keep-alive",
        "host": "localhost:8081",
        "origin": "http://localhost:8100",
        "pragma": "no-cache",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:62.0) Gecko/20100101 Firefox/62.0"
    }

为什么差异很大?我的服务器使用第一个标头。

有人帮我吗?

PHP 请求

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://localhost:8081/api/card/login/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "Content-Type: application/json",
    "access_token: foo",
    "login: 11111111111",
    "senha: 1234"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

离子请求

var headers = new Headers({
            'Content-Type': 'application/json',
            'senha': '1234',
            'login': '11111111111',
            'access_token': 'foo'
        })

        let options = new RequestOptions({
            headers: headers,
            method: RequestMethod.Post,
            withCredentials: true
        })

        this.httpClient.post("http://localhost:8081/api/card/login/", {}, options).subscribe(data => {
            console.log(data['_body'])
        }, error => {
            console.error('API Error : ', error.status)
            console.error('API Error : ', JSON.stringify(error))
        })

标签: phpnode.jsionic-frameworkionic3

解决方案


推荐阅读