Reputation: 515
I want to try and pass a string from AJAX to Flask, the request is being received, but there doesn't seem to be any data coming through
AJAX:
function scan(){
var i = $(".btn.btn-default").attr("id");
var id = i.toString();
console.log(id);
$.ajax({
type: "GET",
url: '/scan',
data: id,
success: function(data) {
console.log(data);
}
});
}
Python / Flask:
if request.method == 'GET':
logger.log('request received') #this is printing out successfully
a = request.data
if a == 'mystring':
return 'Successful'
What am I doing wrong? It must be something incredibly simple.
Upvotes: 0
Views: 1784
Reputation: 2550
To quote the jQuery documentation:
data Type: PlainObject or String or Array Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).
Since you are not supplying a key/value pair to $.ajax
, no data is being sent to the server. Instead, try data: {id: id}
.
Then, in Flask, you can access this value: request.args['id']
(or request.args.get('id', None)
to supply a default value of None
).
(Here is the documentation for Request.args
.)
Upvotes: 1