简体   繁体   English

Angular - HTTP-Post -> 无法识别响应

[英]Angular - HTTP-Post -> Response not recognized

I want to make a REST call in Angular which should return a JWT (which is fake) in response.我想在 Angular 中进行 REST 调用,它应该返回 JWT (这是假的)作为响应。 At least the console logging should be output, at best "localStorage" should be filled.至少控制台日志应该是output,最好填“localStorage”。 The call is actually sent and the REST interface answers.呼叫实际发出,REST 接口应答。 Here is a log from Wireshark.这是来自 Wireshark 的日志。 Only the "console.log (" XXXXXXXX ");"只有“console.log(”XXXXXXXX“);” is written to the console.被写入控制台。 Why is the logging not written?为什么不写日志?

SOLUTION: I passed an incorrect JSON object.解决方案:我传递了一个不正确的 JSON object。

{"id" : "user.id", "username" : "user.username", "firstName" : "user.firstName", "lastName" : "user.lastName", "token" : "fake-jwt-token" }

Failure beforehand:事前失败:

Angular: Angular:

login(username: string, password: string) {console.log("XXXXXXXX");
    return this.http.post<any>(`http://localhost/api/v1/users/authenticate`, "{username=\"" + username + "\",password=\"" + password +"\"}")//return this.http.post<any>(`${config.apiUrl}/users/authenticate`, { username, password })
        .pipe(map(user => {console.log(user);
            // login successful if there's a jwt token in the response
            if (user && user.token) {
                console.log("true :)");
                // store user details and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify(user));
                this.currentUserSubject.next(user);
            } else {
                console.log("false :(")
            }

            return user;
        }));
}

Wireshark:线鲨:

POST /api/v1/users/authenticate HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 44
Accept: application/json, text/plain, */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
Content-Type: text/plain
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://localhost:4200/login
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7

{username="user1",password="secretpassword"}HTTP/1.1 200 OK
Date: Fri, 26 Jun 2020 11:29:20 GMT
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.4.2
X-Powered-By: PHP/7.4.2
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Content-Length: 124
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json; charset=UTF-8

{id: "user.id", username: "user.username", firstName: "user.firstName", lastName: "user.lastName", token: 'fake-jwt-token' }

Are you calling the login with the subscribe method?您是否使用 subscribe 方法调用登录? eg login().subscribe()?例如登录()。订阅()?

post api for login is missing its port(4200):用于登录的帖子 api 缺少其端口(4200):

return this.http.post<any>( http://localhost:4200/api/v1/users/authenticate , { username: username, password: password }) return this.http.post<any>( http://localhost:4200/api/v1/users/authenticate , { username: username, password: password })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM