获取 api 不返回 Location 标头

Posted

技术标签:

【中文标题】获取 api 不返回 Location 标头【英文标题】:fetch api not returning the Location header 【发布时间】:2016-12-25 12:19:31 【问题描述】:

这是我从 chrome Version 52.0.2743.82 (64-bit) 复制的标题(尚未确定这是否与 chrome 隔离)

Request URL:http://localhost:8080/users
Request Method:POST
Status Code:201 
Remote Address:[::1]:8080

Response Headers
view parsed
HTTP/1.1 201
Access-Control-Allow-Origin: http://localhost:9000
Vary: Origin
Access-Control-Allow-Credentials: true
X-Application-Context: application:xenoterracide,development
Location: http://localhost:8080/users/17
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Length: 0
Date: Thu, 18 Aug 2016 13:51:19 GMT

Request Headers
view parsed
POST /users HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 92
Pragma: no-cache
Cache-Control: no-cache
accept: application/json
Origin: http://localhost:9000
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/52.0.2743.82 Safari/537.36
content-type: application/json
DNT: 1
Referer: http://localhost:9000/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

这就是我创建 fetch 客户端的方式

import 'isomorphic-fetch';
import  Router, RouterConfiguration  from 'aurelia-router';
import  Logger  from 'aurelia-logging';
import  Container, LogManager, autoinject  from 'aurelia-framework';
import  Route  from './main/Route';
import  HttpClient  from 'aurelia-fetch-client';

@autoinject
export class App 
router: Router;
private log: Logger = LogManager.getLogger( App );

constructor( container: Container ) 
    let client: HttpClient = new HttpClient;
    client.configure( config => 
        config.useStandardConfiguration()
            .withBaseUrl( 'http://localhost:8080/' )
            .withDefaults( 
                credentials: 'include',
                headers: 
                    Accept: 'application/json'
                
             );
     );
    container.registerSingleton( HttpClient, () => client );

这是我的服务代码

@autoinject
export class RegistrationSvc 
private log: Logger = LogManager.getLogger( RegistrationSvc.name );

constructor( private client: HttpClient ) 


register( user: RegisterUser ): Promise<any> 
    let basic = user.email + ':' + user.passwordCredentials.password;
    let log = this.log;
    log.debug( `registering $ basic ` );

    return this.client.fetch( 'users', 
        method: 'post',
        body: json( user )
     ).then( response => 
        let location = response.headers.get( "Location" );
        log.debug( `location $ location ` );
     );

我在 chrome 的控制台中运行了这个

response.headers.forEach( v => console.log( v ) );
VM783:1 no-cache
VM783:1 no-cache, no-store, max-age=0, must-revalidate
VM783:1 0
undefined

...我的其他标题在哪里?为什么他们不见了?

【问题讨论】:

【参考方案1】:

默认情况下,您可以在 javascript 中从 fetch 响应中获取的唯一标头是:

Cache-Control Content-Language Content-Type Expires Last-Modified Pragma

要公开其他人,请发送一个Access-Control-Expose-Headers 响应标头,其值要么是您要公开的标头的显式列表,要么是* 以公开它们。

【讨论】:

以上是关于获取 api 不返回 Location 标头的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序--location API

为啥 Fetch API 调用不返回响应标头?

React - 带有标头的 ajax GET 请求仍返回 401(未经授权)

在Android中使用FusedLocationApi时如何获取Location的模式

Azure 函数返回不推荐使用的 API 标头

如何使用 Location 标头?