调用Java Servlet时Httpclient响应为null

问题描述 投票:1回答:1

当我尝试在WAS环境中对httpservlet(java)发布帖子时,我很难搞清楚为什么我会在角度中获得null响应。

我想要做的是调用api登录。我想收到响应头,但我得到一个空响应,使得无法对响应做任何事情。

我尝试将我的参数作为表单数据发送到内容类型application / x-www-form-urlencoded; charset = UTF-8和查询参数,没有定义内容类型。

在这两种情况下我都可以登录并在浏览器中获得带有标题信息的200响应。

这是我的电话

    return this.http.post(this.contextRoot.getHost() + 'osa-kantoor-ws/api/auth/login?' + param1 + "&" + param2,null, this.httpOptions).map((res : Response) => {
      localStorage.setItem('currentUser', JSON.stringify(username));
      return res;
    });

我的标题信息现在是

  httpOptions = {
    headers: new HttpHeaders({'Content-Type' : 'application/json', 'Accept':  'application/text'}),
    params: {}
  };

已设置localstorage项,但响应信息为null。

这是我的订阅

 login() {
    this.loginService.login(this.loginForm.value.userName,this.loginForm.value.password)
      .subscribe(
        data => {
          console.log(data);
          this.router.navigate([this.returnUrl]);
        },
        error => {
          console.log(error);
          this.loginfail = true;
        });
  }

当我检查我的响应标题上的浏览器信息时,我得到以下信息

enter image description here

任何人都可以告诉我什么我失踪了?

以下是原始方法

 login(username: string, password: string) {
    let param1: string = encodeURIComponent('userName') + '=' + encodeURIComponent(username);
    let param2: string = encodeURIComponent('password') + '=' + encodeURIComponent(password);
    let formParams: string = param1 + '&' + param2;
    return this.http.post(this.contextRoot.getHost() + 'osa-kantoor-ws/api/auth/login', formParams, this.httpOptions).map(response=> {
      localStorage.setItem('currentUser', JSON.stringify(username));
      console.log(response);
      return response;
    });
  }
angular typescript cors
1个回答
2
投票

谢谢AJT_82,

添加观察:对配置的“响应”是解决方案。

响应现在是我所期待的。默认响应是观察:'body'显然。

我的HTTP配置现在

  httpOptions = {
    headers: new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8','Accept':  'application/xml'}),
    observe: 'response'
  };

我的回答是现在

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.