Let the python bottle framework support jquery ajax RESTful-style PUT and DELETE requests, jqueryrestful

Source: Internet
Author: User

Let the python bottle framework support jquery ajax RESTful-style PUT and DELETE requests, jqueryrestful

In the past two days, the backend Management System was developed using the python bottle framework. The interface agreed to use RESTful requests, and the front-end used jquery ajax to interact with the interface. Both POST and GET requests were normal, when the Request Method uses a PUT or DELETE Request, the "HTTP Error 405: Method Not Allowed" Error is thrown directly. The delete value of the Request Method submitted by ajax is also changed to OPTIONS.

  

Du Niang gave me a lot of answers, either not supported by the browser, or re-encapsulated jquery. There are other methods ...... er... just re-encapsulate jquery. javascript is not very familiar, and other methods have been tried one by one. It is better to start with the python source code.

In the main file. in py, the bottle hook @ hook ('before _ request') function (reference: from bottle import request) is added, and r = request is added to receive the request variable of the bottle, you can view the values in the request in the Debugger view of IDE during debugging. For details, see

  

We can find the environ variable of the request. We can see that when ajax is used for submission, the 'HTTP _ ACCESS_CONTROL_REQUEST_METHOD '(64411616) = {str} 'delete' parameter is longer than the normal one.

  

The value of the Request Method changes to: 'request _ method' (45248088) = 'options'. It is no wonder that the ajax METHOD of jquery is submitted, and the route set by the python bottle framework cannot be received.

  

  

It's much better to know where the problem is.

Method 1: Add the following code directly in the hook to change the value of REQUEST_METHOD.

@ Hook ('before _ request') def validate (): "using hooks to process page or interface access events" "# Let the bottle framework support REQUEST_METHOD = request for RESTful PUT and DELETE requests of jquery ajax. environ. get ('request _ method') HTTP_ACCESS_CONTROL_REQUEST_METHOD = REQUEST. environ. get ('HTTP _ ACCESS_CONTROL_REQUEST_METHOD ') if REQUEST_METHOD = 'options' and HTTP_ACCESS_CONTROL_REQUEST_METHOD: request. environ ['request _ method'] = HTTP_ACCESS_CONTROL_REQUEST_METHOD

 

Method 2: Modify the source code of the bottle call module

Through debug, we can find that in the wsgiref. simple_server module called by bottle. py, the def get_environ (self) method of the class WSGIRequestHandler (BaseHTTPRequestHandler) class is used to set the environ variable of the request.

  

Therefore, you only need to add the processing code before return env. For details, see the following code:

Class WSGIRequestHandler (BaseHTTPRequestHandler): server_version = "WSGIServer/" + _ version _ def get_environ (self): env = self. server. base_environ.copy () env ['server _ Protocol'] = self. request_version env ['server _ soft'] = self. server_version env ['request _ method'] = self. command if '? 'In self. 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 () if host! = Self. client_address [0]: env ['remote _ host'] = HOST env ['remote _ ADDR '] = self. client_address [0] if self. headers. get ('content-type') is None: env ['content _ type'] = self. headers. get_content_type () else: env ['content _ type'] = self. headers ['content-type'] length = self. headers. get ('content-length') if length: env ['content _ length'] = length for k, v in self. headers. items (): k = k. replace ('-','_'). upper (); v = v. strip () if k in env: continue # skip content length, type, etc. if 'HTTP _ '+ k in env: env ['HTTP _' + k] + = ',' + v # comma-separate multiple headers else: env ['HTTP _ '+ k] = v # enable the bottle framework to support jquery ajax RESTful PUT and DELETE requests such as if 'request _ method' in env and env ['request _ METHOD '] = 'options' and 'HTTP _ ACCESS_CONTROL_REQUEST_METHOD' in env: env ['request _ method'] = env ['HTTP _ ACCESS_CONTROL_REQUEST_METHOD '] return env

 

After the python service is restarted, submit ajax again and you will find that the Request is successfully submitted. Check the http Request header. The value of the Request Method is still OPTIONS, but the server can normally receive the Request.

  

 

Copyright:

This article is original and published by AllEmpty in the blog Park. Copyright is jointly owned by the blog Park. You are welcome to reprint this article. You must keep this statement without your consent and provide the original article link clearly on the article page,Otherwise, the right to pursue legal liability is reserved.. If you have any questions, you can use1654937@qq.comThank you very much for contacting me.

Publish the content of this series,MasterYesIt is to learn and make progress together with everyone. If you are interested, you can add Q group: 327360708. Let's discuss it together.

For more information, please refer to blog: http://www.cnblogs.com/EmptyFS/

 

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.