You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Sanctum鉴权:带Authorization头请求出现net::ERR_EMPTY_RESPONSE

Laravel Sanctum鉴权浏览器请求异常排查

我的API采用Laravel Sanctum实现鉴权,调用受auth:sanctum中间件管控的接口时遇到以下异常情况:

  • 在**浏览器环境(基于Ionic+Angular开发的应用)**发起请求,只要添加Authorization请求头,就会触发错误:GET http://myapi.host.com/api/user net::ERR_EMPTY_RESPONSE
  • 不携带Authorization头请求时,接口能正常响应,但返回GET http://myapi.host.com/api/user 401 (Unauthorized),无法完成身份验证
  • 用Postman调用该接口完全正常,初步排除服务器配置问题

带Authorization头的请求代码

const headers = new HttpHeaders({
  Authorization: `Bearer ${await this.apiHelper.getToken()}`
});

this.http
  .get(`${this.uri}/api/user`, {
    headers: headers,
  })
  .subscribe(
    (res) => console.log(res),
    (error) => console.log(error)
  );

对应的错误信息:GET http://myapi.host.com/api/user net::ERR_EMPTY_RESPONSE

不带Authorization头的请求代码

this.http
  .get(`${this.uri}/api/user`)
  .subscribe(
    (res) => console.log(res),
    (error) => console.log(error)
  );

对应的响应:GET http://myapi.host.com/api/user 401 (Unauthorized)


内容的提问来源于stack exchange,提问作者Fran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 21:47:47