![](/img/trans.png)
[英]Angular JS using $http() get a response, with status 200, but data is null
[英]Response with status: 200 Ok for URL: null error for angular2.0 http post
我正在嘗試使用角度2中的http發布數據,我向其中添加了一個添加了api的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>
在服務器響應中添加Access-Control-Allow-Headers
標志。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.