2017-08-08 77 views
2

我有一个严重的金字塔框架问题。 我添加此功能金字塔金字塔CORS doest不能提供PUT和DELETE

add_cors_headers_response_callback(event): 
    def cors_headers(request, response): 
     response.headers.update({ 
     'Access-Control-Allow-Origin': '*', 
     'Access-Control-Allow-Methods': 'POST,GET,DELETE,PUT,OPTIONS', 
     # 'Access-Control-Allow-Methods': 'DELETE, OPTIONS', 
     'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization', 
     'Access-Control-Allow-Credentials': 'true', 
     'Access-Control-Max-Age': '1728000', 
     }) 
    event.request.add_response_callback(cors_headers) 

from pyramid.events import NewRequest 
config.add_subscriber(add_cors_headers_response_callback, NewRequest) 

我可以使用GET,POST方法也求到我的服务器。 问题是当我使用PUT和DELETE方法服务器发送HTTP状态404,但是当我卷曲的路线我得到答案(或使用POSTMAN)。

我在Node.JS中使用了相同的Angular http查询,它接受我的请求。 我不知道为什么金字塔拒绝服务视图

updatePatternModel(newPattern,url) { 
    let fullurl = this.baseUrl + url; 
    let data = { 
     "rule":newPattern 
    }; 
    return this.http.put(fullurl,JSON.stringify(data)) 
     .map(response => response.json()) 
     .catch(err => this.handleErrorObservable(err)); 
    } 

金字塔

@view_config(request_method='PUT', route_name='gm') 
    def gm(self): 
     return dict() 

EDITED

我做出了榜样

from pyramid.view import view_config 

@view_config(route_name='gm', renderer='json',request_method="GET") 
def get(request): 
    return dict(hello="GET") 


@view_config(route_name='gm', renderer='json',request_method="POST") 
def post(request): 
    return dict(hello="POST") 


@view_config(route_name='gm', renderer='json',request_method="PUT") 
def put(request): 
    return dict(hello="PUT") 


@view_config(route_name='gm', renderer='json',request_method="DELETE") 
def delete(request): 
    return dict(hello="DELETE") 

GET/POST的作品,但PUT ,删除不!

import { Injectable } from '@angular/core'; 
import {Http} from "@angular/http"; 

import "rxjs/add/operator/map" 

@Injectable() 
export class AppService { 
    baseUrl : string = "http://10.0.0.34:8880" 
    constructor(public http:Http) { } 

    getData(url) { 
    console.log('hi'); 
    let full_url= this.baseUrl + url; 
    return this.http.get(full_url) 
     .map(response => response.json()); 
    } 
    postData(url) { 
    console.log('hello'); 
    let full_url= this.baseUrl + url; 
    return this.http.post(full_url,null) 
     .map(response => response.json()); 
    } 
    putData(url) { 
    console.log('hello'); 
    let full_url= this.baseUrl + url; 
    return this.http.put(full_url,null) 
     .map(response => response.json()); 
    } 
    deleteData(url) { 
    console.log('hello'); 
    let full_url= this.baseUrl + url; 
    return this.http.delete(full_url) 
     .map(response => response.json()); 
    } 

} 

enter image description here

enter image description here

+1

似乎你的问题不涉及到CORS,因为在这种情况下,应用程序将不会返回404。浏览器只会阻止请求。如果我是你,我会找别的地方。 –

+0

编辑,PLZ审查的问题 –

回答

2

解决由于

@view_config (route_name='gm', renderer='json',request_method="OPTIONS") 
    def options(context, request): 
     request.response.headers.update({ 
      'Access-Control-Allow-Origin': '*', 
      'Access-Control-Allow-Methods': 'POST,GET,DELETE,PUT,OPTIONS', 
      'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization', 
      'Access-Control-Allow-Credentials': 'true', 
      'Access-Control-Max-Age': '1728000', 
     }) 
     return dict() 
+1

也有https://gist.github.com/mmerickel/1afaf64154b335b596e4作为可重复使用的模式,你可以包括在你的应用程序,如果你喜欢。 –