Apache CORS OPTIONS请求状态501错误

时间:2017-09-02 19:01:49

标签: apache angular cakephp cors preflight

Angular 2 http post未设置内容类型

我试图在http.post上设置标题而没有运气。

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';

import { User } from '../_models/index';

@Injectable()
export class UserService {
constructor(private http: Http) { }

create(username: string, firstname: string, lastname: string, email: string, password: string) {
    let headers = new Headers({
        'Accept' : 'application/json',
        'Content-Type': 'application/json'
    });
    let options = new RequestOptions({ headers: headers, method: 'post' });

    return this.http.post(
        'http://127.0.0.1:8080/api/users/register',
        JSON.stringify({username, firstname, lastname, email, password}),
        options
        // this.jwt()
    ).map(
        (response: Response) => response.json()
    );
}

我在apache config上设置了标题

    <Directory /var/www/stockwatch_api>
            Header set Access-Control-Allow-Origin "*"
            Header set Access-Control-Allow-Headers "Content-Type"
            Header set Access-Control-Allow-Methods "POST, PUT, DELETE, GET, HEAD"

我还在我的cakephp api app中设置了相同的标题。

我收到了这个回复:

  

状态代码:501未实现

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用headers.append()

let headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');