2016-06-22 5 views
0

私は角度2でhttpを使用してデータをポストしようとしています。追加されたAPIコールをhttpコールに追加しました。私のDBへのPOSTデータ、cosoleで、それは誤りステータスのレスポンス:200 OK URL:angular2.0のヌルエラーhttp投稿

XMLHttpRequest cannot load http://localhost/a2server/index.php/profile/addprofile. 
Request header field Content-Type is not allowed by Access-Control- 
Allow-Headers in preflight response. 
EXCEPTION: Response with status: 200 Ok for URL: null 



import {Component} from '@angular/core'; 
import {Http, Response, Headers} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 
import {Subject } from 'rxjs/Subject'; 

    @Component({ 
templateUrl: './components/http/http.html' 
}) 

export class HttpSample { 


http: Http; 

postResponse = new Person(); 


constructor(http: Http) { 

    this.http = http; 

} 



onSubmit(){ 

    var headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 
    this.http.post('http://localhost/a2server/index.php/profile/addprofile', JSON.stringify({firstName:'Joe',lastName:'Smith'}),{headers:headers}) 
     .map((res: Response) => res.json()) 
     .subscribe((res:Person) => this.postResponse = res); 
} 
}class Person{ 
firstName:string; 
lastName:string; 
} 

を示しており、私はちょうど私が

<div> 
    <button (click)="postData()">Post Data</button> 


</div> 
+0

[Angular 2でCORS対応のHTTPリクエストを作成する方法は?](http://stackoverflow.com/questions/36768418/how-to-make-cors-enabled-http-requests-in-angular -2) – rinukkusu

答えて

0

は、サーバーの応答にAccess-Control-Allow-Headersフラグを追加ボタンを配置し、私の見解から任意の値を投稿していないです。

+0

stilll no change – MMR

+0

"変更なし" - まったく同じエラーを意味しますか? – rinukkusu

+0

はい同じエラー – MMR

関連する問題