[求助] Angular 无法和后端 API 进行交互,而 Postman 都能够使用。

2019-11-08 17:14:20 +08:00
 Kamitora

前端

报错

Service 代码

environment.API_URL = 'http://localhost:8080'

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/x-www-form-urlencoded',
  })
};
@Injectable({
  providedIn: 'root'
})
export class LoginService {
  private loginVerifyUrl = '/user/verify';
  private fetchALL = '/user/fetch-all';

  constructor(private http: HttpClient) { }
  
  verifyLogin(email: string, password: string): Observable<any> {
    return this.http.post<LoginToken>(environment.API_URL + this.loginVerifyUrl, {email, password}, httpOptions);
  }
}

Consume 代码

onSubmit() {
    this.token.email = this.emailFormControl.value;
    this.token.password = this.passwordFormControl.value;
    this.loginService.verifyLogin(this.token.email, this.token.password).subscribe( 	data => {
        console.log(data, 'is what we got from the server');
    });
  }

后端

函数签名

运行 console

1270 次点击
所在节点    问与答
6 条回复
crs0910
2019-11-08 17:27:03 +08:00
你应该看浏览器请求找问题。
YuxiangLuo
2019-11-08 17:28:52 +08:00
这不是很明显的空指针问题吗
hahastudio
2019-11-08 17:29:54 +08:00
对比浏览器的请求和 Postman 的请求
wszgrcy
2019-11-08 17:52:30 +08:00
angular:出了什么问题都赖我?
url,method,请求头,cookie,请求体(如果有)都对比一遍
Immortal
2019-11-08 17:56:10 +08:00
应该是你代码里的请求和 postman 请求有些不一样 比如参数字段头信息等等 然后服务端数据校验不严格报错了 5xx 基本都是服务端错误的
Kamitora
2019-11-09 01:04:49 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/617682

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX