น่าสนใจ เนื่องจากปรากฎว่าคุณสามารถใช้ request.data
ได้ก็ต่อเมื่อมีการโพสต์ข้อมูลด้วยประเภท mime ที่ flask ไม่สามารถจัดการได้ ไม่เช่นนั้นจะเป็นสตริงว่าง ""
ฉันคิดว่าเอกสารไม่ชัดเจนมาก ฉันทำการทดสอบบางอย่างแล้วและดูเหมือนว่า ในกรณีนี้ คุณสามารถดูคอนโซลเอาต์พุตที่ขวดสร้างขึ้นเมื่อคุณรันการทดสอบของฉัน
ข้อมูลคำขอที่เข้ามา
data
ประกอบด้วยข้อมูลคำขอขาเข้าเป็นสตริงในกรณีที่มาพร้อมกับ mimetype Flask ไม่สามารถจัดการได้
นำมาจาก http://flask.pocoo.org/docs/api/
แต่เนื่องจากเรากำลังทำมาตรฐาน POST
โดยใช้ json flask
สามารถจัดการสิ่งนี้ได้ค่อนข้างดีดังนั้นคุณจึงสามารถเข้าถึงข้อมูลจากมาตรฐาน request.form
นี้ ss=str(request.form)
ควรทำเคล็ดลับตามที่ฉันได้ทดสอบแล้ว
ตามหมายเหตุด้านข้าง @crossdomain(origin='*')
สิ่งนี้ดูอันตราย มีเหตุผลว่าทำไมเราไม่อนุญาตคำขอ ajax ข้ามไซต์ แต่ฉันแน่ใจว่าคุณมีเหตุผลของคุณ
นี่คือรหัสที่สมบูรณ์ที่ฉันใช้ในการทดสอบ:
from flask import Flask
app = Flask(__name__)
from datetime import timedelta
from flask import make_response, request, current_app
from functools import update_wrapper
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
@app.route("/", methods=['POST'])
@crossdomain(origin='*')
def hello():
ss=str(request.form)
print 'ss: ' + ss + ' request.data: ' + str(request.data)
return ss
@app.route("/test/")
def t():
return """
<html><head></head><body>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type='text/javascript'>
jQuery.ajax(
{
type: "POST",
dataType: "json",
data: "adasdasd",
url: 'http://127.0.0.1:5000/',
complete: function(xhr, statusText)
{ alert(xhr.responseText) }})
var oReq = new XMLHttpRequest();
oReq.open("POST", "/", false);
oReq.setRequestHeader("Content-Type", "unknown");
oReq.send('sync call');
alert(oReq.responseXML);
</script></body></html>
"""
if __name__ == '__main__':
app.run()
เอาท์พุท:
$ python test.py
* Running on http://127.0.0.1:5000/
127.0.0.1 - - [07/Aug/2012 02:45:28] "GET /test/ HTTP/1.1" 200 -
ss: ImmutableMultiDict([('adasdasd', u'')]) request.data:
127.0.0.1 - - [07/Aug/2012 02:45:28] "POST / HTTP/1.1" 200 -
ss: ImmutableMultiDict([]) request.data: sync call
127.0.0.1 - - [07/Aug/2012 02:45:28] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2012 02:45:29] "GET /favicon.ico HTTP/1.1" 404 -
และระบบของฉัน:
$ python --version
Python 2.6.1
$ python -c 'import flask; print flask.__version__;'
0.8
$ uname -a
Darwin 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
ใช้กูเกิลโครม Version 20.0.1132.57
person
Samy Vilar
schedule
07.08.2012
ajax
และจัดการข้อมูลตามที่คุณต้องการsuccess(data, textStatus, jqXHR)Function, Array A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
- person wirey00   schedule 07.08.2012console.log(arguments)
.. มันจะให้คุณสมบัติภายในวัตถุแก่คุณ .. แต่ถ้าคุณต้องการจัดการdata
ให้ทำในฟังก์ชันความสำเร็จ - person wirey00   schedule 07.08.2012