开发者

jQuery Validation plugin results in empty AJAX POST

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???


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

Can't you just do

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


I soved the issue, the problems was the url i sued for post. Instead of using a full 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).

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

Which works as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜