任何人都可以在我的新的Angular 4 Project中使用HTTP請求來幫助我使用restful API進行GET,POST和PUT數據。Angular 4 restful API「PUT」和「POST」方法
我設法從API得到我的數據,但是當我嘗試後還是把它總是與
OPTIONS http://pencil-designs.com/codeigniter/index.php/api/Orphans/add_orphan 405 (Method Not Allowed)
XMLHttpRequest cannot load http://pencil-designs.com/codeigniter/index.php/api/Orphans/add_orphan. Response for preflight has invalid HTTP status code 405
Response {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers…}
這裏返回的是我的服務代碼
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http'
import 'rxjs/Rx';
@Injectable()
export class OrphansService {
constructor(private http : Http) { }
private headers = new Headers({
'Content-Type' : 'application/json',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers' : 'Content-Type'
})
// Get all Orphans
getAllOrphans(){
return this.http.get('http://pencil-designs.com/codeigniter/index.php/api/Orphans/getAllOrphans3').map(res => res.json());
}
// Delete an orphan
deleteOrphanApi(orphan_id){
return this.http.put('http://pencil-designs.com/codeigniter/index.php/api/Orphans/update_orphan?orphan_id=' + orphan_id, {headers:this.headers})
}
// Add New Orphan
addOrphanApi(orphan){
return this.http.post('http://pencil-designs.com/codeigniter/index.php/api/Orphans/add_orphan', orphan, {headers:this.headers}).map(res => res.json());
}
}
我嘗試了很多解決方案來發送頭文件,我真的不知道問題出在哪裏,我的代碼或服務器配置,或者API。
這是鏈接到在線應用程序 http://pencil-designs.com/cpms-app/
我卡在這一點,並進入無限循環,任何幫助,請!
的API不允許'OPTIONS '方法。 隨着您刪除請求,您也可以使用'delete'方法。 – sheplu
您可以使用像Advanced REST客戶端這樣的工具來將PUT或POST測試主體發佈到您的服務器端點嗎? – Hodglem
@Hodglem - 這會有什麼好處呢?這是它失敗的OPTIONS請求。 – Quentin