dsh12544 2016-09-14 04:35
浏览 47
已采纳

Codeigniter和Jquery Ajax,发布的数据没有通过

I am having problem getting the post data in the controller when I send the post request via Jquery Ajax, I have checked with firebug and the form post data is being submitted, but in the controller when I do print_r($_POST); it returns an empty array. What could be wrong ?

Here is the relevant code :

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

Controller (index function) (http://mysite/Contact - Localhost-wamp)

public function index (  )
{
    print_r($_POST);
}
  • 写回答

4条回答 默认 最新

  • dounieyan2036 2016-09-14 04:53
    关注

    change data : $( this ).serialize ( ), to data : new FormData($('#contact-form')[0]) remove dataType : 'json' if this didnot work for you please let me know?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示