django-tastypie PATCH给我“ 400(错误请求)”

前端之家收集整理的这篇文章主要介绍了django-tastypie PATCH给我“ 400(错误请求)” 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在Apache上运行Django站点,该站点Nginx实例为前端,以服务我的静态媒体.

我通过django-tastypie将API公开给我需要在其上修补字段的模型.当我执行本地测试(通过django runserver)时,一切正常.但是,在实时服务器上,我返回了“ 400(错误请求)”.

我读过一些地方说Nginx不支持PATCH?那正确吗?有一个好的解决方法吗?难道我做错了什么?

我只发送要通过postData更新的字段.

jQuery代码

  1. $.ajax({url: '...',type: 'PATCH',accepts: 'application/json',contentType: 'application/json',dataType: 'json',data: postData,processData: false,success: function() {
  2. // Success Code!
  3. },error: function() {
  4. // Error Code!
  5. }
  6. });

美味资源:

  1. class ReceivedMessageResource(ModelResource):
  2. """
  3. """
  4. campaign = fields.ForeignKey(CampaignResource,'campaign')
  5. campaign_name = fields.CharField(readonly=True)
  6. campaign_id = fields.IntegerField(readonly=True)
  7. message_type = fields.CharField(readonly=True)
  8. display_date = fields.CharField(readonly=True)
  9. attachments = fields.ToManyField('apps.campaign.api.AttachmentResource','attachment_set',related_name='message',full=True)
  10. class Meta:
  11. queryset = ReceivedMessage.objects.all()
  12. resource_name = 'message'
  13. filtering = {'id': ALL,'campaign': ALL_WITH_RELATIONS}
  14. excludes = ['reason','provider','loyalty_profile','original_message','date_received']
  15. allowed_methods = ['get','post','put','delete','patch']
  16. paginator_class = ReceivedMessagesPaginator
  17. authentication = ApiKeyAuthentication()
  18. authorization = DjangoAuthorization()

如何排序的任何方向将不胜感激:)

最佳答案
如果您正在使用最新版本的TastyPie(自8月5日起,是GitHub存储库中的版本),则可以按照from the documentation的说明进行操作:

Using PUT/DELETE/PATCH In Unsupported Places

Some places,like in certain browsers or hosts,don’t allow the PUT/DELETE/PATCH methods. In these environments,you can simulate those kinds of requests by providing an X-HTTP-Method-Override header. For example,to send a PATCH request over POST,you’d send a request like:

  1. curl --dump-header - -H "Content-Type: application/json" -H "X-HTTP-Method-Override: PATCH" -X POST --data '{"title": "I Visited Grandma Today"}' http://localhost:8000/api/v1/entry/1/

因此,如果您的主机不支持方法,请添加X-HTTP-Method-Override标头以及您要执行的方法名称.

猜你在找的Nginx相关文章