我try 使用AJAX请求的Success和Failure块.我的函数正在工作,我可以很容易地直接指向控制器,并使用我的查询来修改我的SQL数据库,例如.一切正常,这就是我之前没有注意到AJAX响应的原因.
正如我所说的,我可以毫无问题地执行我的操作,但是我的AJAX总是返回失败,即使我的操作正在工作.我不明白怎么修,你能帮我看看吗?
error个
一开始,我试着把这个记录下来
console.log('Question sending failed:', result.message);
发送ProductDetail?ProductID=7:2879的问题 失败:未定义
然后我试着把这个记录下来
console.log('the entire result: ', result);
整个ProductDetail?ProductID=7:2880 结果:真
如果我同时记录这两个日志(log),结果就是
整个ProductDetail?ProductID=7:2880 结果:真
您可以看到代码中的更改
action个
[HttpPost]
[Authorize]
public async Task<IActionResult> SendEmployeeQuestion(SetEmployeeQuestionQuery request)
{
var result = await MediatrSend(request);
return Json(result);
}
returns
对于"Return Json(Result)",也是"true"
query个
public class SetEmployeeQuestionQuery : IRequest<bool>
{
public ClaimModel ClaimModel { get; set; }
public int ProductId { get; set; }
public int FirmId { get; set; }
public string QuestionText { get; set; }
}
public class SetEmployeeQuestionQueryHandler : IRequestHandler<SetEmployeeQuestionQuery, bool>
{
private readonly IECommerceProductRepository _eCommerceProductRepository;
public SetEmployeeQuestionQueryHandler(IECommerceProductRepository eCommerceProductRepository)
{
_eCommerceProductRepository = eCommerceProductRepository;
}
public async Task<bool> Handle(SetEmployeeQuestionQuery request, CancellationToken cancellationToken)
{
DateTime currentDateTime = DateTime.Now;
string formattedTimestamp = currentDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
var questionId = _eCommerceProductRepository.InsertToEmployeeQuestion(request.ClaimModel.EmployeeId, formattedTimestamp).Result;
await _eCommerceProductRepository.InsertToAnswerLog(null, questionId, request.FirmId, request.QuestionText, null);
return true;
}
}
script
// Ask to seller modal
$(document).ready(function () {
$('.ask-button').on('click', function () {
$('#askToSellerModal').modal('show');
});
$('#askToSellerModal').on('hidden.bs.modal', function () {
clearTextarea();
});
$('#sendQuestionButton').on('click', function () {
var productId = "@Model.ProductData.Id";
var firmId = "@Model.ProductData.FirmId";
var questionText = $('#askToSellerTextArea').val();
$.ajax({
type: 'POST',
url: '@Url.Action("SendEmployeeQuestion")',
data: {
ProductId: productId,
FirmId: firmId,
QuestionText: questionText
},
success: function (result) {
if (result.success) {
console.log('Question sent successfully:', result.message);
console.log('the entire result: ', result);
} else {
console.log('Question sending failed:', result.message);
console.log('the entire result: ', result);
}
},
error: function (error) {
console.error('AJAX error:', error.statusText);
}
});
clearTextarea();
$('#askToSellerModal').modal('hide');
});
});
function clearTextarea() {
document.getElementById('askToSellerTextArea').value = '';
}