【问题标题】:Codeigniter & Jquery Ajax, Post data not going throughCodeigniter 和 Jquery Ajax,发布数据未通过
【发布时间】:2017-01-21 19:02:57
【问题描述】:

当我通过 Jquery Ajax 发送发布请求时,我在控制器中获取发布数据时遇到问题,我已经检查了 firebug 并且正在提交表单发布数据,但是当我执行 print_r($_POST) 时在控制器中;它返回一个空数组。有什么问题?

以下是相关代码:

形成 HTML

<form id="contact-form" class="form-horizontal subscribe" accept-charset="utf-8" action="<?= base_url ( 'Contact/' ); ?>" method="post">
    <!--Name-->
    <div class="form-group">
        <label class="sr-only" for="name">Name</label>
        <div class="input-group">
            <div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
            <input id="name" type="text" class="form-control validate" name="name" placeholder="Your full name" value="">
        </div>
    </div>
    <!--Email-->
    <div class="form-group">
        <label class="sr-only" for="email">Email</label>
        <div class="input-group">
            <div class="input-group-addon"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></div>
            <input id="email" type="text" class="form-control validate" name="email" placeholder="Your email address" value="">
        </div>
    </div>
    <!--Message-->
    <div class="form-group">
        <label class="sr-only" for="message">Message</label>
        <div class="input-group">
            <div class="input-group-addon"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></div>
            <textarea id="message" name="message" class="form-control" rows="3" placeholder="Message"></textarea>
        </div>
    </div>
    <!--Submit Button-->
    <div class="form-group text-right">
        <input id="contact_us" type="hidden" name="contact_us" value="Submit" />

        <button type="submit" id="contact_us" class="btn btn-warning" name="contact_us" value="Submit">
            Send &nbsp;<span class="glyphicon glyphicon-send" aria-hidden="true"></span>
        </button>
    </div>
</form>

JAVASCRIPT

<script>
$( '#contact-form' ).submit ( function ( event ) {

    event.preventDefault (  );
    event.stopPropagation (  );

    var $scriptUrl = $( '#contact-form' ).attr ( 'action' );

    $.ajax ( {
        method : 'POST',
        url : $scriptUrl,
        data    : $( this ).serialize ( ),
        cache : false,
        processData: false,
        contentType: false,
        dataType : 'json',
        success : function ( data, textStatus, jqXHR ) {
            if ( data.success === true ) { alert ('success'); }
            else { alert ('failure'); }
        },
        error : function ( jqXHR, textStatus, errorThrown ) {
            alert (  jqXHR.responseText  );/*This returns the empty array*/
        }
    } );

} );
</script>

控制器(索引函数) (http://mysite/Contact - Localhost-wamp)

public function index (  )
{
    print_r($_POST);
}

【问题讨论】:

    标签: javascript php jquery ajax codeigniter


    【解决方案1】:

    data : $( this ).serialize ( ), 更改为 data : new FormData($('#contact-form')[0]) 删除 dataType : 'json' 如果这对您不起作用,请告诉我?

    【讨论】:

    • 这将适用于图像上传到... serialize() 在提交图像时可能有问题...
    • data : $( this ).serialize ( ) 更改为data : new FormData($('#contact-form')[0]) 确实有效,但您能解释一下$('#contact-form')[0] 吗?
    【解决方案2】:

    如果您使用的是 jquery,那么您应该使用如下语法:

    $.post('url',$("#contact-form").serialize(),function(data){
     //here take action on returned data
    });
    

    【讨论】:

      【解决方案3】:

      您只需更改
      data : $(this).serialize(),

      data : $('#contact-form').serialize(),

      在 ajax 中 $this 不起作用,因为当您在 ajax 中调用 $this 时,$this 总是调用父对象 ajax。

      【讨论】:

      • 如果萤火虫显示正在传输的帖子数据,这意味着data : $('#contact-form').serialize (),正在工作。无论如何我尝试了你的更改,但这也没有用:(
      【解决方案4】:

      学生 x。这可能不是您的答案,但这就是您传递表单所需的全部内容

      $(function() {
      "use strict";
      $("#form1").submit(function() {
          var data = $("#form1").serialize();
          //alert(data); return false;
          $.ajax({
              url: "/forms/form1",
              data: data,
              type: "POST",
              success: function(msg) {
                  if (msg) {
                      $("#display").html(msg);
                  } else {
                      $("#display").text("nothing came back For some reason");
                  }
              }
          });
          return false;
      });
      

      });

      您也可以使用它。我将它用于我的所有表单,然后我只需要 1 个脚本。当然你会改变成功。只需使用 ajax 的 ID 命名所有表单

      (function() {
      "use strict";
      $('form#ajax').on('submit', function() {
          var that = $(this),
              url  = that.attr('action'),
              type = that.attr('method'),
              data = {};
          that.find('[name]').each(function(index, value) {
              var that   = $(this),
                  name   = that.attr('name'),
                  value  = that.val();
              data[name] = value;
          });
      $.ajax({
          url: url,
          type: type,
          data: data,
          success: function(response) {
              $('#display').html(response).delay(8000).fadeOut(1000);
          }
        });
      return false;
      

      }); })(jQuery);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        • 1970-01-01
        相关资源
        最近更新 更多