<分区>
我正在尝试执行模型的更新功能,尝试通过 PUT 调用 API 中的 Update 方法。
这是我的服务客户:
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, RequestMethod, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
[...]
updateClients(client_id, data){
let headers = new Headers({ 'Content-Type': 'application/json' });
let body = JSON.stringify(data);
return this.http.put(`${this.baseUrl}/client/`+client_id, body, headers)
.toPromise()
.then(response => response.json().data)
.then(items => items.map((item, idx) => {
return{
id: item.id
};
}));
}
但它不起作用,因为该方法通过 OPTIONS 而不是 PUT。在我的浏览器控制台中出现:XMLHttpRequest can not load
http://ip/api/client/5 。飞行前响应中的 Access-Control-Allow-Methods 不允许方法 PUT。
但是,在 API (Laravel 5.3) 中启用了 PUT 方法。我已经用 POSTMAN 试过了,它很管用。在我的响应标题中:
Access-Control-Allow-Origin: *
Allow: GET, HEAD, PUT, PATCH, DELETE
Cache-Control: no-cache
Connection: keep-alive
为什么它来自期权请求而不是放置? POST 方法适用于 Angular 2。