我不知所措,我无法理解当POST,PUT和GET工作正常时DELETE请求是如何通过的。而且我非常确定我已经完成了CORS工作所需的所有配置。我使用的是AngularJS和Flask-CORS扩展
这是我目前的工作:
Angular Config:
angular.module('...' [...]).config(function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
Angular工厂:
angular.module('...').factory('...'), function($http) {
return {
deleteElectronicAddress: function (partyId, contactMechanismId) {
return $http({
url: urlBase + 'party/' + partyId + '/contact-mechanism/' + contactMechanismId,
method: 'DELETE',
});
},
someMoreMethods: { ... }
}
}
我的Flask代码,(我使用Miguel Grinberg在他的书Flask Web Development上提出的应用程序结构)
config.py
class Config:
...
CORS_HEADERS = 'Content-Type'
CORS_RESOURCES = {r"/api/*": {"origins": "*"}}
CORS_METHODS = ['GET', 'POST', 'DELETE', 'PUT', 'OPTIONS', 'HEAD']
...
project_folder /应用/ __ INIT __ PY:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.cors import CORS
from config import config
db = SQLAlchemy()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
db.init_app(app)
CORS(app)
from .api_1_0 import api as api_1_0_blueprint
app.register_blueprint(api_1_0_blueprint, url_prefix='/api/v1.0')
return app
我目前正在使用Flask-CORS v1.7.4。
project_folder /应用程序/ API / party_contact_mechanisms.py:
from datetime import date
from flask import jsonify
from .. import db
from ..models import PartyContactMechanism
from . import api
@api.route(
'/party/<int:party_id>' +
'/contact-mechanism/<int:contact_mechanism>', methods=['DELETE'])
def unlink_party_contact_mechanism(party_id, contact_mechanism):
"""Unlink the contact mechanism onto the party."""
party_contact = PartyContactMechanism.query \
.filter_by(party_id=party_id) \
.filter_by(contact_mechanism_id=contact_mechanism) \
.first()
party_contact.thru_date = str(date.today())
db.session.commit()
return jsonify(success=True)
我已经在本地计算机和局域网上连接的其他计算机上使用httpie进行了测试,它运行正常。
我使用0.0.0.0主机配置运行角度和烧瓶,以便可以在网络上连接的其他计算机上访问它。
这是从浏览器调用时的请求和响应标头
请求标题:
OPTIONS /api/v1.0/party/32232/contact-mechanism/80667 HTTP/1.1
Host: 10.61.18.217:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://0.0.0.0:9000
Access-Control-Request-Method: DELETE
Connection: keep-alive
响应标题:
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Allow: POST, OPTIONS, DELETE
access-control-allow-origin: *
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Content-Type
Content-Length: 0
Server: Werkzeug/0.9.6 Python/2.7.6
Date: Mon, 18 Aug 2014 07:20:18 GMT
这是从httpie:
调用时的请求和响应标头请求标题:
DELETE /api/v1.0/party/32232/contact-mechanism/80667 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, compress
Content-Length: 0
Host: 10.61.18.217:5000
User-Agent: HTTPie/0.8.0
响应标题:
HTTP/1.0 200 OK
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, POST, PUT
Access-Control-Allow-Origin: *
Content-Length: 21
Content-Type: application/json
Date: Mon, 18 Aug 2014 07:31:05 GMT
Server: Werkzeug/0.9.6 Python/2.7.6
{
"success": true
}
答案 0 :(得分:1)
我得到了我现在做错的事,
实际上我的project_folder/app/api/party_contact_mechanisms.py
文件上有另一条路线,我没有费心添加我的样本,因为我觉得它无关紧要(我错了)
这里是:
@api.route(
'/party/<int:party_id>' +
'/contact-mechanism/<int:contact_mechanism>',
methods=['POST'])
@cross_origin(methods=['POST'])
def link_party_contact_mechanism(party_id, contact_mechanism):
"""Link the contact mechanism onto the party."""
party_contact = PartyContactMechanism(
party_id=party_id, contact_mechanism=contact_mechanism,
from_date=str(date.today()), thru_date=None
)
db.session.add(party_contact)
db.session.commit()
return jsonify({})
你可以看到它有相同的路由规则,但使用不同的方法,在这里它的POST。
在我使用Flask-CORS v1.7之前,我使用的是v1.3,这解释了@cross_origin
装饰器,因为这是启用路由CORS的唯一方法(或者如果你想要的话)在你的所有路线上,你把它放在你的before_request
)
新版本让我错过的是,是的,您现在可以通过初始化来启用整个应用程序CORS,就像我在project_folder/app/__init__.py
上做的那样,是的,您仍然可以通过` @ cross_origin&#39;,但是当你这样做时,它会覆盖全局配置,在我的情况下,我有两条路由具有相同的规则但是方法不同,只有一条具有@cross_origin装饰器,并且只有一条路径具有问题
我刚刚将其删除,现在效果很好。警告在你的另一条路线上添加另一个@cross_origin
装饰器不会修复它,它似乎只读取它的第一次出现。