Python 如何使用jquery发送带有GET请求的有效负载?
我能够使用cURL&Python Flask使用有效负载发送和接收下面的GET请求;但无法使用jquery执行此操作Python 如何使用jquery发送带有GET请求的有效负载?,python,jquery,flask,Python,Jquery,Flask,我能够使用cURL&Python Flask使用有效负载发送和接收下面的GET请求;但无法使用jquery执行此操作 curl --location --request GET 'http://127.0.0.1:5000/image' \ --header 'Content-Type: application/json' \ --data-raw '{"task_id": "7f05f454-385a-415f-9bfb-225d77a16365"}' Jquery: $.ajax(
curl --location --request GET 'http://127.0.0.1:5000/image' \
--header 'Content-Type: application/json' \
--data-raw '{"task_id": "7f05f454-385a-415f-9bfb-225d77a16365"}'
Jquery:
$.ajax({
type: "GET",
url: 'http://localhost:5000/image',
data : JSON.stringify(body),
contentType: 'application/json',
success: function(response){
console.log("GET returned successfully");
console.log(response);
document.getElementById("divGetResponse").innerHTML = JSON.stringify(response);
},
error: function(error){
console.log("GET failed");
console.log(error);
}
});
我的服务器端代码如下所示:
@app.route('/image', methods=['GET'])
def getStatus():
data = request.get_data()
if not data:
response = make_response(json.dumps('Invalid task_id'), 400)
response.headers['Content-type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
if 'task_id' not in request.json or len(request.json['task_id']) == 0:
response = make_response(json.dumps('Invalid task_id'), 400)
response.headers['Content-type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
task = conversionBgTask.AsyncResult(request.json['task_id'])
if task.info is None:
response = make_response(jsonify({'task_id' : '<task_id not found>'}), 204)
response.headers['Content-type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
response = make_response(jsonify(task.info), 200)
response.headers['Content-type'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/image',methods=['GET'])
def getStatus():
数据=请求。获取数据()
如果没有数据:
response=make_response(json.dumps('Invalid task_id'),400)
response.headers['Content-type']='application/json'
response.headers['Access-Control-Allow-Origin']='*'
返回响应
如果'task_id'不在request.json或len中(request.json['task_id'])==0:
response=make_response(json.dumps('Invalid task_id'),400)
response.headers['Content-type']='application/json'
response.headers['Access-Control-Allow-Origin']='*'
返回响应
task=conversionBgTask.AsyncResult(request.json['task\u id'])
如果task.info为无:
response=make_response(jsonify({'task_id':''}),204)
response.headers['Content-type']='application/json'
response.headers['Access-Control-Allow-Origin']='*'
返回响应
response=make_response(jsonify(task.info),200)
response.headers['Content-type']='application/json'
response.headers['Access-Control-Allow-Origin']='*'
返回响应
我知道GET应该只有查询字符串参数。但是,如果我需要设计这样的API,我如何使用jquery执行这样的请求呢
注意:我是web开发的初学者:)您可以使用GET请求发送有效负载,但不能使用浏览器,如jQuery、axios或fetch 在您的示例中,因为它只是一个参数,所以可以将其作为查询字符串发送。但正如克劳斯所说,您的API设计需要更改。如果您应该设计API,请正确设计。让自己了解休息的概念。这会有帮助的。