- ajax 和 JSON.stringify($(“form”).serialize())
$.ajax({
url: $SCRIPT_ROOT + '/login',
type: "post",
contentType : "application/json",
data: JSON.stringify($("form").serialize()),
dataType: 'json',
success: function (data) {
$("#name").text(data.result);
}
})
后台接收:
data = request.get_json()
print(type(data),data) #<class 'str'> username=2&password=2(url格式)
# 将url转换成dict
import urllib.parse
data = dict(urllib.parse.parse_qsl(data))
print(type(data), data) #<class 'dict'> {'password': '2', 'username': '2'}
a = data.get('username')
b = data.get('password')
print(a,b)
return jsonify(result=int(a) + int(b))
2.ajax 和 $(“form”).serialize()
$.ajax({
url: $SCRIPT_ROOT + '/login',
type: "post",
data: $("form").serialize(),
dataType: 'json',
success: function (data) {
$("#name").text(data.result);
}
})
后台接收:
data = request.get_data(as_text=True)
print(type(data),data) #<class 'str'> username=2&password=2(url格式)
# 将url转换成dict
import urllib.parse
data = dict(urllib.parse.parse_qsl(data))
print(type(data), data) #<class 'dict'> {'password': '2', 'username': '2'}
a = data.get('username')
b = data.get('password')
print(a,b)
return jsonify(result=int(a) + int(b))
3.ajax 和 obj
$.ajax({
url: $SCRIPT_ROOT + '/login',
type: "post",
data: $("form").serialize(),
dataType: 'json',
success: function (data) {
$("#name").text(data.result);
}
})
后台接收:
data = request.get_data(as_text=True)
print(type(data),data) #<class 'str'> username=2&password=2
a = data.get(' ')
b = data.get(' ')
print(a,b)
return jsonify(result=int(a) + int(b))
4.ajax 和 json数据
$.ajax({
url: $SCRIPT_ROOT + '/login',
type: "post",
data: $("form").serialize(),
dataType: 'json',
contentType : "application/json",
success: function (data) {
$("#name").text(data.result);
}
})
后台接收:
data = request.get_json()
print(type(data),data) #<class 'str'> username=2&password=2
a = data.get(' ')
b = data.get(' ')
print(a,b)
return jsonify(result=int(a) + int(b))