我正在使用HttpModule来调用我的API。我已将模块包含在我的导入中,并在启动我的服务器时抛出错误(pl查看screeshot)。 Error Image
我正在尝试使用Http.post从服务中调用我的API并获取上面显示的错误
我的app.module.ts:
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {HeaderComponent} from './header/header.component';
import {AuthComponent} from './auth/auth.component';
import {MarksComponent} from './marks/marks.component';
import {SigninComponent} from './auth/signin/signin.component';
import {SignupComponent} from './auth/signup/signup.component';
import {FormsModule} from '@angular/forms';
import {AppRoutingModule} from './app-routing.module';
import {AuthService} from './auth/auth.service';
import {HttpModule} from '@angular/http';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
AuthComponent,
MarksComponent,
SigninComponent,
SignupComponent
],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpModule
],
providers: [AuthService],
bootstrap: [AppComponent]
})
export class AppModule {
}
我的服务:
import {Router} from '@angular/router';
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
@Injectable()
export class AuthService {
constructor(private router: Router, private http: Http) {
}
signUpUser(firstName: string, lastName: string, email: string, password:
string) {
const body: [] = [firstName, lastName, email, password];
return this.http.post('http://localhost:3000/auth/signup', body).map(data
=> {
data = data.json();
return data;
});
}
}
答案 0 :(得分:0)
在我看来,这不是一个http问题,而是一个糟糕的声明:
用它来定义body:
let body = {conge,user};
this.httpClient.post('http://localhost:8080/api/sendconge', body)
.subscribe(function(data) {
console.log(data);
});
httpClient比简单的http更好地发送数据。
答案 1 :(得分:0)
我以错误的方式使用post方法。 我的更新代码是:
signUpUser(firstName: string, lastName: string, email: string, password: string) {
// const body: [] = [firstName, lastName, email, password];
const body = {
firstName: firstName,
lastName : lastName,
email : email,
password : password
};
return this.http.post('http://localhost:3000/auth/signup', body);
}
答案 2 :(得分:-1)
你应该为body数组提供一个类型。
例如:
const body:any [] = [firstName,lastName,email,password];