參考了這篇文章:https://segmentfault.com/q/1010000000473230 後報錯誤:Getting Error “Form submission canceled because the form is not connected”
然後在:http://stackoverflow.com/questions/42053775/getting-error-form-submission-canceled-because-the-form-is-not-connected 找到了答案,增加了 $(document.body).append(form);後問題解決。
具體原理如下:
Details : According to the HTML standards, if the form is not associated browsing context(document), form submission will be aborted.
HTML SPEC see 4.10.21.3.2
In Chrome 56, this spec was applied.
var url = "__ROOT__/index.php/home/ApiInfo/register"; requestData = {'email': email,'pwd':pwd};
$.post(url, requestData, function (data) {
if(data != null){
alert(data);
}else{
var form = $("<form method='post'></form>");
form.attr({"action":"__ROOT__/index.php/home/ApiInfo/sucess"});
$(document.body).append(form);
form.submit();
}
});
/////////////////////////////////////////
後來,用Form提交,AJAX驗證,用另外的方法實現了:
var url = "__ROOT__/index.php/home/ApiInfo/isExist";requestData = {'email': email};var isOk = true;$.ajax({ url:url, type:'get', data:requestData, async: false,//關鍵操作,需要等到結果才執行後面操作 success:function(data){ alert(data); isOk = false; }});return isOk;