私はログイン用のページを作成していますが、投稿要求に問題があります。 私は角度の最終バージョン2角2投稿の返信サービスからの返信
login.htmlと
<label for="username">User</label>
<input type="text" id="user" #user>
<label for="password">Password</label>
<input type="password" id="password" #password>
<button type="submit" (click)="onLogin(user.value, password.value); user.value=''; password.value=''">Login</button>
login.tsを使用しています
@Component({
selector: 'login',
templateUrl: 'app/views/login.html',
providers: [ LoginService ],
styleUrls: [ 'css/login.css' ]
})
export class Login extends Locale {
users: User[];
constructor(public localization: LocalizationService,
private _loginService: LoginService) {
super(null, localization);
}
onLogin(username: string, password: string) {
if (!username && !password){return;}
this._loginService.login(username, password)
.subscribe(
user => this.users.push(user),
error => console.log(error),
() => console.log("Complete")
);
}
}
user.ts
export class User {
constructor(
public username: string,
public password: string) {}
}
user.servece.ts
@Injectable()
export class LoginService {
// URL to web api
private loginUrl = './authenticate';
constructor (private http: Http) {}
login (username, password): Observable<User> {
var body = `username=${username}&password=${password}`;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
var options = new RequestOptions({ headers: headers });
console.log("Url: "+this.loginUrl);
console.log("body: " + body);
console.log("headers: "+headers);
return this.http.post(this.loginUrl, body, options)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
私がエラーを持っている: POST http://localhost:3000/authenticate 404(見つかりません)
データをリンクするときだろうJSONファイルに登録して保存、送信する必要がありますlocalhost:3000/authenticate。
何が問題なのですか、助けてください!
サーバーはこのURL +ポート –