我正在尝试将数据发布到服务器。我的php方面运行正常。当我尝试使用postman post工作时。但从角度来看,我得到405(方法不允许)错误:
zone.js:2935 OPTIONS http://angularslim.local/public_html/users 405 (Method Not Allowed)
Failed to load http://angularslim.local/public_html/users: Response for preflight has invalid HTTP status code 405
。
我的代码如下: 在我的服务中,我有以下代码。
@Injectable()
export class AuthService {
constructor(private http: Http) {}
register(user:User){
this.http.post("http://angularslim.local/public_html/users", user).subscribe((res: Response) => {
console.log("inside");
})
}
在我的php部分,我确实有这些行
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
答案 0 :(得分:1)
您是否尝试通过JSON.stringify(用户)传递表单值。我以前遇到过同样的问题。后来我发现我的问题是那个。
this.http.post("your api url", JSON.stringify(user))
.subscribe(
(val) => {
console.log("POST call successful value returned in body", val);
});
希望这适合你。
答案 1 :(得分:0)
您需要允许交叉起源和方法
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
另请尝试https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
另外,我建议您使用HttpClient(不推荐使用http)和HttpResponse
答案 2 :(得分:0)
If you use Slim, this is mostly because your Javascript code sends HTTP OPTIONS request while there is no route that handle HTTP OPTIONS.
You need to add a route that handle OPTIONS request or modify any Javascript code that cause preflight request to be sent (by making it a simple request. Take a look at this question Why is an OPTIONS request sent and can I disable it?).
To add route that handle OPTIONS
$app->options('/users', function ($request, $response, $args) {
//do something here
]);
More information: