我已将代码简化为仅显示此示例的相关代码。我可以提交表单,但我需要了解如何在出现错误时获得响应,以便我可以解析它并显示错误。
这是我到目前为止所尝试的内容:
$.ajax( URL, {
'data': data,
'method': 'PUT'
}
}),
.done( function() {
// yay!
})
.fail( function() {
// How can I get the failure response and parse this?
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 0 :(得分:0)
$(document).on('click', '#yourFormButton', function(e) {
e.preventDefault();// catch the form's submit event
var one = $('#yourvalueone').val();
var two = $('#yourvaluetwo').val();
var three = $('#yourvaluethree').val();
$.ajax({url: 'YourScript.php',
data: {one:one,two:two,three:three },
type: 'post',
async: 'true',
//dataType: 'jsonp',//can specify type we will return html
beforeSend: function() { //catch before send maybe load spinner
console.log('before');
},
complete: function(data) { //capture complete
console.log('complete');
},
success: function (data) {//it successful place returned HTML from PHP script in DIV
$('#DivForYourResults').html(data);
}
});
});
将其发送到您的php脚本,只需回显“hello”,这将返回到您的页面。然后尝试将数据放入数据库等