Let Python bottle framework support jquery Ajax's restful-style put and delete requests

Source: Internet
Author: User

These two days in the Python bottle Framework for the development of a background management system, the interface convention using RESTful style requests, the front end uses jquery Ajax to interact with the interface, the use of post and get requests are normal, and the request Method directly explodes "HTTP error 405:method not allowed" error when using a put or delete request. The request method value of Ajax submissions has also become the options.

  

Degrees Niang a lot of answers, either the browser does not support, or say that they re-encapsulate jquery, there are other ways ... Uh... Re-encapsulation jquery Even if the JavaScript is not very familiar with the impossible, and then other methods have been tried and no use, want to go or from the Python source code to start a little better.

Just do it. In the main file main.py, add the bottle hook @hook (' before_request ') function (reference: from bottle import request), and then add R = Request, To receive the request variable of the bottle, it is convenient to view the value of the request in the IDE's debugger view when debugging, see

  

We find the Environ variable for REQUEST, and we can see that the ' Http_access_control_request_method ' (64411616) = {str} ' DELETE ' parameter is submitted more than usual when using AJAX submissions

  

The value of the REQUEST METHOD becomes: ' Request_method ' (45248088) = ' OPTIONS ', no wonder jquery's AJAX approach is submitted, and the Python bottle framework-defined route is never received

  

  

It's better to know the problem is to be reformed.

Method One: Add the following code directly in the hook to change the value of the Request_method

@hook ('before_request')defValidate ():"""using hooks to handle page or interface access events"""    #let the bottle Framework support jquery Ajax's restful-style put and delete requestsRequest_method = Request.environ.get ('Request_method') Http_access_control_request_method= Request.environ.get ('Http_access_control_request_method')    ifRequest_method = ='OPTIONS'  andhttp_access_control_request_method:request.environ['Request_method'] = Http_access_control_request_method

Method Two: Modify the source code of the bottle calling module

With Debug, we can find that bottle.py calls the Def get_ of Class Wsgirequesthandler (Basehttprequesthandler) classes in the Wsgiref.simple_server module. The Environ (self) method is used to set the Environ variable of the request

  

So we just need to add the processing code before the return env, see the code below

classWsgirequesthandler (Basehttprequesthandler): Server_version="wsgiserver/"+__version__    defGet_environ (self): env=self.server.base_environ.copy () env['Server_protocol'] =self.request_version env['Server_software'] =self.server_version env['Request_method'] =Self.commandif '?' inchSelf.path:path,query= Self.path.split ('?', 1)        Else: Path,query= Self.path,"'env['Path_info'] = urllib.parse.unquote (path,'iso-8859-1') env['query_string'] =Query Host=self.address_string ()ifHost! =Self.client_address[0]: env['Remote_host'] =host env['REMOTE_ADDR'] =Self.client_address[0]ifSelf.headers.get ('Content-type') isnone:env['Content_Type'] =Self.headers.get_content_type ()Else: env['Content_Type'] = self.headers['Content-type'] Length= Self.headers.get ('Content-length')        iflength:env['content_length'] =length forKvinchSelf.headers.items (): K=k.replace ('-','_'). Upper (); v=V.strip ()ifKinchenv:Continue                    #Skip content length, type,etc.            if 'HTTP_'+kinchenv:env['HTTP_'+k] + =','+v#comma-separate Multiple Headers            Else: env['HTTP_'+K] =v#let the bottle Framework support jquery Ajax's restful-style put and delete requests        if 'Request_method' inchEnv andenv['Request_method'] =='OPTIONS'  and 'Http_access_control_request_method' inchenv:env['Request_method'] = env['Http_access_control_request_method']        returnEnv

After restarting the Python service, we can submit the Ajax again, we will find the submission is successful, check the HTTP request header, the value of the request method or options, but the server has been able to receive the request

  

Copyright Notice:

This article by Allempty original and published in the blog Park, copyright and Blog Park Common All, welcome to reprint, without my consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility . If you have any questions, you can contact me by [email protected] , thank you very much.

published this part of the content, the Lord is to work together with everyone to learn common progress, interested friends can gaga Q Group: 327360708, we discuss together.

For more information, please note the blog:http://www.cnblogs.com/EmptyFS/

Let Python bottle framework support jquery Ajax's restful-style put and delete requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.