简体   繁体   English

Angular 2 http发布返回200,但未返回任何响应

[英]Angular 2 http post is returning 200 but no response is returned

My http call is returning 200 but no response is captured. 我的http呼叫返回200,但未捕获任何响应。 My code inside subscribe is not being hit . 我的订阅里面的代码没有被击中 The API is returning data when I test in postman. 当我在邮递员中测试时,API返回数据。 Here is my code. 这是我的代码。

getToken(authcode: string) {

        var data = 'client_id=InspectWebApp_client&code=' + authcode + '&redirect_uri=http://localhost:3000&grant_type=authorization_code';
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        let options = new RequestOptions({ headers: headers });
        this.http.post('https://fedloginqa.test.com/as/token.oauth2', data, options)
            .subscribe((res: Response) => {

                var resultsToken = res.json();
               localStorage.setItem("access_token",resultsToken.access_token)
                //return this.inspections;
            })

    }

I was also facing the same problem. 我也面临着同样的问题。 The problem was solved using the map function on Observables. 使用Observables上的map函数解决了该问题。 Here is my implementation: 这是我的实现:

login(Username:string, Password:string) : Observable<Response>{
    let headers = new Headers();
    headers.append("Authorization", "Basic " + btoa(Username + ":" + Password)); 
    headers.append("Content-Type", "application/x-www-form-urlencoded");
    return this._http.post(this._baseUrl+"auth/login", " " , {headers: headers}  )
        .map((response: Response) => {
            return response;     
        }).catch(this.handleError);
}

Here the handleError is a function to catch the excceptions generated. 这里的handleError是捕获生成的感知的函数。 This is a function in login.service.ts that sends the username and password to the api to get data. 这是login.service.ts中的一个函数,该函数将用户名和密码发送到api以获取数据。 You can see that I am returning response from the map function in this service. 您可以看到我正在从此服务中的map函数返回响应。 Now, this returned response can be caught in subscribe function in following way: 现在,可以通过以下方式在订阅函数中捕获此返回的响应:

this._loginService.login(this.username, this.password)  
        .subscribe(
            (response) => {
                //Here you can map the response to a type.
                this.apiResult = <IUser>response.json();
            },
            (err) => {
                //Here you can catch the error
            },
            () => {this.router.navigate(['home'])}
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jersey HTTP响应200可以,但是返回内容错误(检查请求) - Jersey HTTP response 200 OK but returning content wrong (checking request) 即使返回200响应,也使用来自Angular的Bing映射REST失败 - Using Bing maps REST from Angular failing even though 200 response returned Jersey HTTP响应200可以,但是返回的内容错误(用户名/密码错误) - Jersey HTTP response 200 OK but returning content wrong (username/password incorrect) Angular 2中的HTTP GET触发我的HTTP POST中收到的登录响应 - HTTP GET in Angular 2 triggers a response received in my HTTP POST for Login 困惑:AngularJS $ http POST响应返回索引文件的HTML内容 - Confusion: AngularJS $http POST response is returning HTML content of index file Angular 5的HTTP POST(获取JWT)在Response中返回NULL - HTTP POST from Angular 5 (to get a JWT) returns NULL in the Response 200个状态码可以作为POST请求的响应吗 - Can 200 status code be response for POST request request.post返回HTTP 200 python - requests.post return HTTP 200 python Angular 8 Interceptors - 如何处理状态 200 中的响应 - Angular 8 Interceptors - How to process response in status 200 200 的 HTTP 请求在 Angular 5 中触发“catch”操作 - HTTP request of 200 triggers "catch" operation in Angular 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM