jQuery Validation插件导致空的AJAX POST

时间:2022-11-13 19:45:38

I have tried to solve this issue for at couple of days with no luck. I have a form, where I use the validation plugin, and when I try to submit it, it submits empty vars. Now, if I remove the validation, everything works fine. Here is my implementation:

我试图在没有运气的情况下解决这个问题几天。我有一个表单,我使用验证插件,当我尝试提交它时,它提交空变量。现在,如果我删除验证,一切正常。这是我的实施:

$(document).ready(function(){

 $("#myForm").validate({
  rules: {
    field1: {
      required: true,
      minlength: 5
    },
    field2: {
      required: true,
      minlength: 10
    }
  },
  messages: {
    field1: "this is required",
    field2: "this is required",
  },
  errorLabelContainer: $("#validate_msg"),
  invalidHandler: function(e, validator) {
    var errors = validator.numberOfInvalids();
    if (errors) {
        $("#validate_div").show();
    }
  },
  onkeyup: false,
  success: false,
  submitHandler: function(form) { doAjaxPost(form); }
 });
});

function doAjaxPost(form) {
  // do some stuff
  $(form).ajaxSubmit();
  return false;
}

As I wrote this does not work when I have my validation, BUT if I remove that, and just add an

正如我写的那样,当我进行验证时,这不起作用,但如果我删除它,只需添加一个

onsubmit="doAjaxPost(this.form); return false";

to my HTML form, it works - any clue???

到我的HTML表单,它的工作原理 - 任何线索???

2 个解决方案

#1


0  

Why do you even need doAjaxPost? And I guess the return false might be the culprit. Do you really need that?

为什么你甚至需要doAjaxPost?而且我认为回报错误可能是罪魁祸首。你真的需要吗?

Can't you just do

你能不能这样做

...
submitHandler: function(form) {
    //do some stuff
    $(form).ajaxSubmit();
}
...

#2


0  

I soved the issue, the problems was the url i sued for post. Instead of using a full URL:

我解决了这个问题,问题是我起诉的网址。而不是使用完整的URL:

var url = "https://" + document.domain + "/ajax.php";

I just use:

我只是用:

var url = "/ajax.php";

and everything works as it should.

一切都按预期工作。


When using php with cookies, the session ID will automatically be sent in the request headers even for Ajax XMLHttpRequests. If you use or allow URL-based php sessions, you'll have to add the session id to every Ajax request url, OR use a relative URL path (like above solution).

使用带cookie的php时,即使对于Ajax XMLHttpRequests,会话ID也会自动发送到请求头中。如果您使用或允许基于URL的php会话,则必须将会话ID添加到每个Ajax请求URL,或使用相对URL路径(如上面的解决方案)。

<script language="javascript" type="text/javascript">
//<![CDATA[
  var url = 'https://'+document.domain+'/ajax.php?<?=session_name(); ?>=<?=session_id(); ?>';
//]]>
</script>

Which works as well.

哪个也有效。

#1


0  

Why do you even need doAjaxPost? And I guess the return false might be the culprit. Do you really need that?

为什么你甚至需要doAjaxPost?而且我认为回报错误可能是罪魁祸首。你真的需要吗?

Can't you just do

你能不能这样做

...
submitHandler: function(form) {
    //do some stuff
    $(form).ajaxSubmit();
}
...

#2


0  

I soved the issue, the problems was the url i sued for post. Instead of using a full URL:

我解决了这个问题,问题是我起诉的网址。而不是使用完整的URL:

var url = "https://" + document.domain + "/ajax.php";

I just use:

我只是用:

var url = "/ajax.php";

and everything works as it should.

一切都按预期工作。


When using php with cookies, the session ID will automatically be sent in the request headers even for Ajax XMLHttpRequests. If you use or allow URL-based php sessions, you'll have to add the session id to every Ajax request url, OR use a relative URL path (like above solution).

使用带cookie的php时,即使对于Ajax XMLHttpRequests,会话ID也会自动发送到请求头中。如果您使用或允许基于URL的php会话,则必须将会话ID添加到每个Ajax请求URL,或使用相对URL路径(如上面的解决方案)。

<script language="javascript" type="text/javascript">
//<![CDATA[
  var url = 'https://'+document.domain+'/ajax.php?<?=session_name(); ?>=<?=session_id(); ?>';
//]]>
</script>

Which works as well.

哪个也有效。