简体   繁体   English

405上载文件时不允许使用方法

[英]405 Method not allowed when uploading file

I have a class-based view with a defined post method, which is the end-point of a file upload. 我有一个带有定义的post方法的基于类的视图,该方法是文件上传的终点。

The request is made using the ng-file-upload module. 该请求是使用ng-file-upload模块发出的。

When executing the upload request, I receive a 405 Method not allowed error. 执行上传请求时,我收到405 Method not allowed错误。

If I try to make a POST to that same URL with the same parameters (except for the file), it's working fine. 如果我尝试使用相同的参数(文件除外)对相同的URL进行POST,则工作正常。

I can see in Firebug that the Response Headers are different: 我在Firebug中看到响应头是不同的:

  • When sending the request via POST, the response's allowed method are POST and OPTIONS 通过POST发送请求时,响应的允许方法是POST和OPTIONS
  • When sending via upload, they are GET, HEAD and OPTIONS 通过上传发送时,它们是GET,HEAD和OPTIONS

What could be causing this? 是什么原因造成的?

[edit] As requested, here is the (simplified) code of the view handling method: [edit]根据要求,以下是视图处理方法的(简化)代码:

def order_data(order, request):
    """
    Return a serialized order with added permission information
    """
    data = OrderSerializer(order).data

    # Add a few custom fields on the data dict

    return data

class SaveOrder(APIView):

    def post(self, request):
        data = request.data.get('order')

        if data.get('id', None) is not None:
            if not request.user.has_perm('orders.modify_order'):
                return HttpResponseForbidden()
            else:
                order = Order.objects.get(id=data['id'])
        else:
            if not request.user.has_perm('orders.create_order'):
                return HttpResponseForbidden()
            else:
                order = Order()

        # Fill in order using the fields in data

         order.save()

        return JsonResponse(order_data(order, request))

[edit2] Actually the behaviour is not exactly what I've described earlier: [edit2]实际上,这种行为与我之前所描述的完全不一样:

  • With upload to url /order/save-order I get an error saying that I need a trailing slash 将其upload到url /order/save-order我收到一条错误消息,要求我在末尾加斜杠
  • With upload to url /order/save-order/ I get a 405 随着upload到URL /order/save-order/我得到了405
  • With post to url /order/save-order it's working fine (original code) 通过post到url /order/save-order可以正常工作(原始代码)
  • With post to url /order/save-order/ I get a 404 post URL /order/save-order/我得到一个404

原来我的请求实际上是针对2个不同的网址完成的,因此上传失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM