我也面臨這樣的問題
step1 set Headers in php file
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
採樣功能,你可以得到響應
public function createAction(Request $request) {
$data=file_get_contents('php://input');
var_dump($data);//here you print you 'POST' or 'GET' Response.
step2 service.ts formService.ts
private headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'});
private insertdataUrl = 'api-url';
//create function that is called by component.html file
create(user: ApiDashboard) {
return this.http.post(this.insertdataUrl,user).map(response => response.json());
}
//here in my code ApiDashboard is model
step3 define you service function in component.ts file
export class ApiDashboardformComponent implements OnInit {
model: any = {};
loading = false;
constructor(private router: Router,
private formService: FormService
) { }
ngOnInit() {
}
Create() {
this.loading = true;
this.formService.create(this.model)
.subscribe(
data => {
this.formService.success('Registration successful', true);
this.router.navigate(['/material-dashboard']);
},
error => {
this.formService.error(error);
this.loading = false;
});
}
}
step4 finally call create() function component.html file
<div class="col-md-6 col-md-offset-3">
<h2>Register</h2>
<form name="form" (ngSubmit)="f.form.valid && Create()" #f="ngForm" novalidate>
訂閱它,請在發佈成功,失敗和功能齊全的 – Dhyey
我的console.log對不起,我真的不知道如何記錄這些功能。當我記錄結果時,我得到以下錯誤: XMLHttpRequest無法加載http://mywebsite.net/contact_me.php。對預檢請求的響應不會通過訪問控制檢查:請求的資源上不存在「訪問控制 - 允許來源」標頭。因此不允許訪問Origin'http:// localhost:4200'。 –
您的服務器腳本是否支持CORS請求?換句話說,是否啓用了CORS? –