你好,我是Angular的新手,我正试图向我的后端(Symfony2.8)发送主题,电子邮件和消息。当我路由 localhost:4200 / contact 时,我没有收到任何错误,没有删除,也没有发生任何事情,URL变为 localhost:4200 /联系? 。这是我的组件,服务和html文件:(顺便说一句,我用这个方法添加和对象到数据库,它正常工作)
export class ContactComponent {
contactForm: any;
constructor(private formBuilder: FormBuilder, public router: Router ,private cService: ContactService) {
this.contactForm = this.formBuilder.group({
'subject': ['', Validators.required],
'email' : ['', Validators.required],
'msg': ['', Validators.required],
});
}
sendMessage()
{
// console.log(this.contactForm.value.subject,this.contactForm.value.email,this.contactForm.value.msg);
this.cService.sendMsg(this.contactForm.value.subject,this.contactForm.value.email,
this.contactForm.value.msg)
.subscribe(
response => {
alert("Thanks for contacting us");
},
error => {
alert(error);
}
);
export class ContactService {
url:string = '/web/contactus';
constructor (private http: Http) {
}
sendMsg(subject: string,email: string, msg: string): Observable<any> {
let body = JSON.stringify({
subject,
email,
msg
} || null);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
console.log(body);
return this.http.post(this.url, body, options)
.map(res => res.json().data)
.do(data => console.log(JSON.parse(JSON.stringify(data || null ))))
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || { };
}
<form [formGroup]="contactForm" (submit)="sendMessage()">
<div class="form-group">
<label >Subject</label>
<input class="form-control" formControlName="subject" placeholder="Subject"/>
<span *ngIf="!contactForm.controls.subject.valid">required</span>
</div><!-- /.form-group -->
<div class="form-group">
<label>E-mail</label>
<input class="form-control" formControlName="email" placeholder="Email"/>
<span *ngIf="!contactForm.controls.email.valid">required</span>
</div><!-- /.form-group -->
<div class="form-group">
<label >Message</label>
<textarea class="form-control" formControlName="msg" rows="6"></textarea>
<span *ngIf="!contactForm.controls.msg.valid">required</span>
</div><!-- /.form-group -->
<button class="btn btn-primary pull-right" type="submit" [disabled]="!contactForm.valid">Post Message</button>
</form>
答案 0 :(得分:0)
看起来它正在抛出一些异常。在控制台中单击保留日志,然后尝试相同。你可能会看到一些例外