我正在使用handontable在Web应用程序上模拟电子表格。我想将用户输入电子表格中的json数据发送到我的数据库。
HTML按钮:
<button id="send_post" class="btn size-medium bg-green text-white shadow hover-moveup">Send Post Request</button>
Js:
document.getElementById("send_post").addEventListener("click", function(event) {
$.ajax({
url: 'http://127.0.0.1:8000/',
headers: { "X-CSRFToken": csrf_token },
type: "POST",
dataType: "json",
data: JSON.stringify(hot.getSourceData()),
statusCode: {
200: function (your_Response_Data) {
console.log(JSON.stringify(hot.getSourceData()))
},
// ... handle errors if required
404: function () {
}
},
complete: function (jqXHR, textStatus) {
}
});
})
views.py
@login_required(login_url='/login/')
def index(request):
user_id = request.user
if request.is_ajax():
if request.method == "POST":
received_json_data = json.loads(request.body)
return HttpResponse(received_json_data, content_type="application/json")
端子:
[04/Apr/2019 01:19:23] "POST / HTTP/1.1" 302 0
我在视图中接收到数据,但是Httpresponse并未将数据返回给浏览器。
答案 0 :(得分:0)
通过在成功的POST请求后设置location.href来解决该问题
statusCode: {
200: function (your_Response_Data) {
console.log(JSON.stringify(hot.getSourceData()));
location.href = "/";
},