我的金字塔框架存在严重问题。 我将此功能添加到金字塔
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 AND 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());
}
}
答案 0 :(得分: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()