回答有点晚了,但这看起来是为您遇到的类似问题添加我的解决方案的最佳位置。
有类似的情况,需要使用 .ajax POST html 响应,它包含错误消息。
从 Chrome 浏览器开始的问题,在提交后不允许异步发布。我们有一个长期运行的过程,需要显示状态。因此,我们的状态跟踪器在提交过程中进行了异步调用,在大多数浏览器中都可以正常工作,但最近在 chrome 中停止工作了。
-一种解决方案是使用 iframe 并以这种方式调用其他服务器端代码。不适合我。
-或者更改服务器端代码以返回 json 并以这种方式处理内容,更多的工作是重写已经到位的客户端和服务器代码。
我在提交帖子并处理 Html 响应时尝试解决 Chrome 的异步调用问题。
这是我想出的,并且似乎正在工作。
去了这么多网站,获得了这么多的想法和知识,我不能直接归功于任何人。
所以我基本上把 $(form).submit() 变成了一个 ajax 帖子。但这在 chrome 和其他试图处理返回的 html 响应的浏览器中仍然存在问题。
最大的问题是重新连接按钮事件和 javascript。
希望这会对某人有所帮助。
if (IsChrome()) {
$.ajax({
type: 'POST',
url: oo.url,
data: oo.data, // serialized form $(form).serialize(); althought .serializeArray() also seems to work here.
dataType: "html",
cache: false,
async: true,
success: function (result, status, xhr) { HandleChromePostAlreadySubmittedSuccess(result, status, xhr); }, // this being the most important one to handle.
error: function (jqXHR, textStatus, errorThrown) { HandlePostAlreadySubmittedError(jqXHR, textStatus, errorThrown); },
complete: function (jqXHR, textStatus) { HandlePostAlreadySubmittedComplete(jqXHR, textStatus); }
});
} else {
// will POST(even though load is for GETs).
// Chrome does not like the hack way. not sure why, the async status calls stop firing.
// IMPORTANT use .serializeArray() below - will force a POST instead of a GET.
// be careful using the jq LIVE on the submit button, may cause multi events to fire.
// The Complete func below, was not really needed(mines an empty func), unlike the chrome above.
$.load(oo.url, $(form).serializeArray(), function (responseText, textStatus, XMLHttpRequest) { HandlePostAlreadySubmittedComplete(XMLHttpRequest, textStatus); });
}
HandleChromePostAlreadySubmittedSuccess = function (result, status, xhr) {
// Chrome - this is suppose to be the correct way.
// Other browsers decided to program write and close a different way, causing the page to unload, from what i understand.
document.write(result);
document.close();
};