[英]AJAX Post not sending form data
I have the following AJAX POST
below, for some reason looking at my logging (server side) the request it's sending is is blank {}
not JSON data is sent. 我在下面有以下
AJAX POST
,出于某种原因查看我的日志记录(服务器端),它发送的请求是空白{}
而不是发送JSON数据。 I have exhausted all possible server side issues this is a client side issue with the script not sending data. 我已经用尽所有可能的服务器端问题这是客户端问题,脚本不发送数据。 why?
为什么?
bootstrap-wizard.js found here -> GitHub bootstrap-wizard.js在这里找到 - > GitHub
My page code overrides the script submit: 我的页面代码会覆盖脚本提交:
<script src="{{ STATIC_URL }}js/bootstrap-wizard.js"></script>
<script type="text/javascript">
$(function() {
var options = {width:1000};
var wizard = $("#some-wizard").wizard(options);
$("#open-wizard").click(function() {
wizard.show();
});
wizard.on("submit", function(wizard) {
$.ajax({
url: '/api/v1/rewards/campaigns/',
type: 'POST',
contentType: 'application/json; charset=UTF-8',
data: $('#wizard').serialize(),
beforeSend: function (request) {
request.setRequestHeader("X-CSRFToken", $('input[name="csrfmiddlewaretoken"]').val());
},
success: function(data, textStatus) {
wizard.submitSuccess(); // displays the success card
wizard.hideButtons(); // hides the next and back buttons
wizard.updateProgressBar(0); // sets the progress meter to 0
console.log('success');
},
error: function(errorThrown){
// data = JSON.parse(errorThrown.responseText);
wizard.submitError(); // display the error card
wizard.hideButtons(); // hides the next and back buttons
console.log(errorThrown);
}
});
});
});
</script>
This is my form: 这是我的表格:
<form action="" method="post" class="form-horizontal" id="wizard" enctype="application/json" >
{% csrf_token %}
<div class="wizard" id="some-wizard">
<h1>{% trans "Setup Wizard" %}</h1>
<div class="wizard-card" data-cardname="card1">
<h3>{% trans "General" %}</h3>
etc, etc <=== all my form fields here
</div>
serialize()
returns key/value formatted URL encoded data (x-www-form-urlencoded), not JSON. serialize()
返回键/值格式的URL编码数据(x-www-form-urlencoded),而不是JSON。 If your server side requires JSON then you need to change your data parameter: 如果您的服务器端需要JSON,那么您需要更改数据参数:
$.ajax({
...
data : JSON.stringify({ input_a : 'value a', input_b : 'value b' }),
...
});
See this question for how to convert a form into JSON automatically. 有关如何自动将表单转换为JSON,请参阅此问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.