目标:
将我的 $.post 更改为 $.ajax 以同步它并显示消息,直到请求完成。
我想确切地知道如何用 ajax 请求做到这一点,我的大问题是当我尝试用 $.post 替换 div 内容时
代码:
景色
function NewVersion() {
$.ajax({
url: "/Valoration/NewVersion",
type: "POST",
async: false,
success: function (data, status, xhr) {
if (data.success) {
$.post(data.hvmHeaderPartialView, function (partial) { $('#divHvmHeader').html(partial); });
MessageNewVersionSucced();
}
},
error: function (xhr, status, err) {
alert(err);
}
});
Controller
public ActionResult HvmHeaderPartialView()
{
return PartialView("_HvmHeaderPartialView,", DetailHvmModel);
}
private ActionResult NewVersion()
{
var result = hvmService.addNewVersion(hvm);
var HvmHeaderPartialView = Url.Action("HvmHeaderPartialView,");
return Json(new
{
success = result,
hvmHeaderPartialView= HvmHeaderPartialView,
});
}
最佳答案
需要保持async=false吗?
你可以使用 $.load()替换 div 内容而不是使用 $.post
。例如:
$('#divHvmHeader').load('ajax/test.html');
带有回调函数:
$('#divHvmHeader').load('/Valoration/NewVersion', function() {
MessageNewVersionSucced();
});
注意:MessageNewVersionSucced
将仅在对“/Valoration/NewVersion”的ajax 调用完成后执行。
关于c# - 如何像 $.post 一样用 $.ajax 替换 div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13913852/