【发布时间】:2018-10-22 09:42:49
【问题描述】:
我遇到了一个奇怪的问题...
预期的行为是:如果请求成功,页面会显示一个简单的警报(只是为了测试它是否正常工作)。如果失败,它应该在 div 中显示错误消息。
$(function() {
$('.error').hide();
$("#form").on('submit', function(e) {
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'POST',
data: $(this).serialize(),
url: '{{ route("admin.categories.store") }}'
})
.done(function(data) {
$('.error').hide();
alert(data.message.name); //simple alert just to check if it works
})
.fail(function(jqXHR) {
errorMessage = jqXHR.responseText;
$('.error').text(errorMessage . "<br>"); // not sure why this line is making the DONE part stop working.
$('.error').show();
});
});
});
如果我让它失败,它会工作,但在成功的情况下,它只会返回给我原始 json:
{"message":{"_token":"lkqlTHGPQBWEVIAQvO8pWs8ORAsSifGyR2TDBEPo","name":"test","submit":"创建 类别"}}
我注意到,如果我删除 $('.error').text(errorMessage . "<br>"); 部分,错误就会消失,“成功”场景会起作用。
有什么想法吗?
【问题讨论】: