代码之家  ›  专栏  ›  技术社区  ›  monstertjie_za

获取http响应头和内容角6

  •  0
  • monstertjie_za  · 技术社区  · 7 年前

    我有下面的web服务器方法,它将数据返回到我们的前端应用程序。

    [FunctionName("SearchCustomerBySearchTerm")]
    public static async Task<HttpResponseMessage> SearchCustomerBySearchTerm([HttpTrigger(AuthorizationLevel.Function, WebRequestMethods.Http.Get, Route = "Customer/SearchCustomerBySearchTerm/{searchTerm}/pageSize/{pageSize}")]HttpRequestMessage req, TraceWriter log, string searchTerm, int pageSize)
    {
        try
        {
            var continuationToken = req.Headers.TryGetValues("continuationToken", out IEnumerable<string> values) ? values.FirstOrDefault() : null;
            PagedResponse<CustomerSearchResult> pagedResponse = await _customerComponent.FindCustomerBy(searchTerm, continuationToken, pageSize);
    
            if (pagedResponse == null) return req.CreateResponse(HttpStatusCode.NoContent, $"Could not find any data related to {searchTerm}");
    
            HttpResponseMessage responseMessage = req.CreateResponse(HttpStatusCode.OK, pagedResponse.Results);
            responseMessage.Content.Headers.Add("continuationToken", pagedResponse.Continuation);
            responseMessage.Content.Headers.Add("Access-Control-Expose-Headers", "*");
    
            return responseMessage;
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
            return req.CreateResponse(HttpStatusCode.InternalServerError, "Something went wrong. Could not search for customers");
        }
    }
    

    通过添加 Access-Control-Expose-Headers .

    从我的角度应用程序中,我正在执行以下请求:

    searchCustomersPaged(searchTerm: string, continuationToken: string): Observable<HttpResponse<CustomerSearchResult>> {
        let customHeaders = new HttpHeaders().set("continuationToken", this.currentContinuationToken);
        const url = "http://localhost:7071/api/Customer/SearchCustomerBySearchTerm/andrew/pageSize/10";
        const parsedUrl = encodeURI(url);
        return this.http.get<HttpResponse<CustomerSearchResult>>(parsedUrl, { headers: customHeaders });
      }
    

    如你所见,我期待着 HttpResponse<CustomerSearch> 回来。

    以下是我尝试阅读标题的方式:

    nextClikcedHandle(continuationToken: string): void {
    this.customerService.searchCustomersPaged(this.customerService.searchTerm, this.currentContinuationToken)
    .subscribe(resp => {
      //add current continuation token, to previous now, as this will be used for 'previous' searching
      this.previousContinuationTokens.push(this.currentContinuationToken);
    
      //set next continuation token received by server
      this.currentContinuationToken = resp.headers.get('continuationToken');
    
      //return search results
      this.customerService.searchResults.next(resp.body);
    });
    

    }

    有了上面的代码, resp.headers 还有 resp.body 总是 undefined . 为什么会这样?

    如果我看看 Network 在chrome中,我可以看到我的数据被返回,以及我的标题。

    enter image description here

    我做错什么了?

    1 回复  |  直到 7 年前
        1
  •  0
  •   monstertjie_za    7 年前

    我找到一篇有用的文章 here :

    默认情况下,httpclient返回响应的主体。你可以 传入一个对象,并将观察键设置为response to的值 得到完整的回应。这对检查 页眉:

    所以我修改了代码如下 observe 关键。

    searchCustomersPaged(searchTerm: string, continuationToken: string): Observable<HttpResponse<CustomerSearchResult>> {
        let customHeaders = new HttpHeaders().set("continuationToken", this.currentContinuationToken);
        const url = "http://localhost:7071/api/Customer/SearchCustomerBySearchTerm/andrew/pageSize/10";
        const parsedUrl = encodeURI(url);
        return this.http.get<CustomerSearchResult>(parsedUrl, { headers: customHeaders, observe: 'response' });
      }
    

    在更改了上述方法后,我可以按常规查询正文和标题:

    nextClikcedHandle(continuationToken: string): void {
    this.customerService.searchCustomersPaged(this.customerService.searchTerm, this.currentContinuationToken)
    .subscribe(resp => {
      //add current continuation token, to previous now, as this will be used for 'previous' searching
      this.previousContinuationTokens.push(this.currentContinuationToken);
    
      //set next continuation token received by server
      this.currentContinuationToken = resp.headers.get('continuationToken');
    
      //return search results
      this.customerService.searchResults.next(resp.body);
    });
    
    推荐文章