angular 同样方法请求两不同接口 request headers不同

老哥们 我用同样的请求方式请求两个接口 但发现request headers不同 并且其中一个请求失败

这是我封装的请求方法

  get(url, params: Object = {}): Observable<HttpResponse> {
    let httpParams = new HttpParams();
    Object.keys(params).map(key => {
      httpParams = httpParams.set(key, params[key]);
    });
    if (this.localStorage.getItem('wechat_access_token')) {
      const httpHeaders = new HttpHeaders({
        'Access-Token': this.localStorage.getItem('wechat_access_token'),
        'Access-Control-Allow-Origin': '*',
      });
      return this.http.get<HttpResponse>(environment.host + url, {
        params: httpParams,
        headers: httpHeaders
      });
    } else {
      return this.http.get<HttpResponse>(environment.host + url, {
        params: httpParams,
      });
    }
  }

第一个接口发起请求 和 request header

  getProduct(productId: any, shopId: string): Observable<HttpResponse> {
    return this.get('shop/index/product', {
      id: productId,
      shop_id: shopId
    });
  }


clipboard.png

第二个接口发起请求 和 request header

  addProducttwo(shopId: string, id: any, unit: number): Observable<HttpResponse> {
    return this.get(`shop/cart/add-product`, {
      shop_id: shopId,
      id: id,
      unit: unit
    });
  }

clipboard.png

发现两次请求的request header不一样 其中一个的access-token没有加上 导致请求失败 请问大佬们这是什么原因啊

阅读 2.6k
1 个回答

找到问题了 是涉及到跨域 其中一个接口OPTIONS请求 后端500的原因

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进