On jQuery.post I get a message: GET method not allowed for requested URL

I have the following problem:

I am working on a flash application and I want to transfer some data to the server via AJAX. I am new to this AJAX job, so I can't get anything like that.

On my client side, when the user clicks on the icon, I want to pass some data through jQuery.post stored in the variable message:

jQuery("#icon_ID").click(function() { var message = { 'GRAPH_TYPE': graphType }; var _sendOnSuccess = function () { } var jqxhr = jQuery.post('/graph', message, _sendOnSuccess, 'json'); }); 

On my server side, I have the following code:

 @app.route('/graph', methods = ['POST']) @login_required def physical_graph(): ret_data = request.form['GRAPH_TYPE'] return "" 

All I want to do now is access the server side GRAPH_TYPE. However, when I click on the icon, I get an error message:

Method not allowed

The GET method is not allowed for the requested URL.

I really don't understand why Python tells me that I use the GET method, when in fact I use the POST method.

Can anyone help me with this? What should I do to solve this problem? If there is any other method that I can use, feel free to give me any advice. Just keep in mind that besides jQuery, I don't want to use another JavaScript library.

Thank you in advance!

+4
source share
1 answer

This is because you pass object as data as

 var message = { 'GRAPH_TYPE': graphType }; 

In this case, jQuery tries to URL-encode the object and by default sends application/x-www-form-urlencoded; charset=UTF-8 with the data type application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8 ans sends a GET request. To overcome this problem, make sure you pass the jQuery string for the data parameter, and for this you can use JSON.stringify as

 var message = JSON.stringify({ "GRAPH_TYPE": graphType }); 
+1
source

All Articles