jQuery验证插件不发送POST数据 [英] Jquery validation plugin does not send POST data

查看:158
本文介绍了jQuery验证插件不发送POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新2:我发现了什么是错的!有一个301重定向的.htaccess文件。我将它张贴作为一个答案,一旦我允许(10岁以下代表用户必须等待8小时)。

Update 2: I found out what was wrong! There was a 301 redirect in the .htaccess file. I will post it as an answer once I am allowed to (users under 10 rep have to wait 8 hours).

更新:我已Barmar的建议,并检查网络卡(A卡,我不太熟悉),发现我从 handle.php接收301 见截图。我会做一些搜索和后我的结果。

Update: I have taken Barmar's suggestion and checked the network tab (a tab I'm not too familiar with) and noticed I am receiving a 301 from handle.php See screenshot. I am going to do some searching and post my results.

原贴:我使用的是 JQuery的验证插件验证和发送通过AJAX表单数据。问题不在于数据被发送,但表单处理程序是说有$ _POST数组中没有的元素。我已经测试了一些不同的方法来发送Ajax和数据发送,但表单处理程序没有看到任何$ _ POST []值。

Original Post: I am using the JQuery validation plugin to validate and send form data via ajax. The problem isn't that the data is being sent, but the form handler is saying there are no elements in the $_POST array. I have tested a few different methods to send ajax, and the data sends, but the form handler does not see any $_POST[] values.

注:我使用jQuery验证插件,因此必须由.validate.submitHandler处理()。任何 $(形式)。在()是不够的。

Note: I have to use the JQuery validation plugin so it has to be handled by .validate.submitHandler(). Any $(form).on() won't suffice.

<form action="handle.php" class="sky-form sky-form-modal" id="sky-form-modal" method=
"post" name="sky-form-modal">
  <label class="input">
  <input name="name" placeholder="Name" type=
  "text">
  </label>
  <label class="input"><input name="company" placeholder="Company" type=
   "text">  
  </label>

  <footer>
      <button class="button" type="submit">Send request</button>
      <div class="progress"></div>
  </footer>
</form>

<script>
$("#sky-form-modal").validate({
  submitHandler: function(form) {    
    var $form = $("#sky-form-modal"); //being explicit for testing
    var $inputs = $form.find("input, select, button, textarea");
    var serializedData = $form.serialize();

    request = $.ajax({
      url: "handle.php",
      type: "POST",
      data: serializedData
    });

    console.log('data: ' + serializedData);
    request.done(function(response, textStatus, jqXHR) {
      console.log("Response: " + response);
     });
   },
});
</script>

handle.php:

<?php
    if(isset($_POST['name'])) {
        echo 'we got it';
    } else {
        echo 'name not set';
    }
?>

好了,好像一切正​​常,检查出的console.log后,我填写了用户名和离开公司的空白:

Okay, so it seems like everything works, check out the console.log after I fill in the username and leave the company blank:

data: name=testtest&company=
Response: name not set

正如你所看到的,序列化的作品,并抓住所有的信息,但是当handle.php处理它告诉我, $ _ POST [] 是空的。通过它handle.php循环证明了这一点:

As you can see, serialize works and grabs all the info, but when handled by handle.php it tells me that the $_POST[] is empty. Looping through it on handle.php proves it:

 foreach($_POST as $key=>$value) { 
   echo "$key: $value 
   \n"; 
}

不返回的。 我也曾尝试 ajaxSubmit会() form.submit(),但我得到了相同的确切的结果。

Which doesn't return at all. I have also tried ajaxSubmit() and form.submit() but I get the same exact results.

这一个看起来我的权利,因为我已经搜查,搜查计算器和整个大多数的问题,这是来了包括名称属性输入标签,它已经完成了。

This one looks right to me, because I have searched and searched stackoverflow and came across that most of the problems with this is including the 'name' attribute on the input tags, which is already done.

在此先感谢!

推荐答案

我的问题是不相关的我的code和结束是的几个声明中的.htaccess 。它重定向我从一个PHP文件到一个目录(prettier URL)的。现在,这是一种常见的技术,以便:

My issue was irrelevant to my code and ended being a few declarations in the .htaccess. It was redirecting me from a .php file to a directory (for prettier URLS). Now, this is a common technique so:

如果您正在使用别人的项目,您的网址的不规范与文件扩展名,检查的.htaccess!

if you are working on someone else's project and your URL's aren't standard with a file extension, check the .htaccess!

这篇关于jQuery验证插件不发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆