【问题标题】:How to set serialize data to ajax $.post method如何将序列化数据设置为 ajax $.post 方法
【发布时间】:2017-07-14 23:09:03
【问题描述】:

我第一次尝试$.post() 方法,已经看过here 的文档,但无法将任何数据发送到我的php 文件。

我的 js 文件上的代码:-

    submitHandler: function (form) {
    var data = $(form).serialize();
    $.post("test.php", function( data ){
        //console.log(data);
    });
    return false;
}

我知道我做错了什么,但无法弄清楚。在我的 php 文件中,var_dump($_REQUEST) 给了我空数组。

【问题讨论】:

  • $.post("test.php", data, function( response ){ //console.log(response); }); return false; }
  • 成功了 :),谢谢@Anant
  • Rajesh Dey 欢迎 :):)
  • 如何从 php 文件中接收任何值。 if($updateUserResult){ $response['userId'] = $userId; } json_encode($response); ,在最近的代码中,我的控制台区域@Anant 是空的。 $updateUserResultboolean

标签: php ajax post


【解决方案1】:

您需要将数据作为第二个参数传递。响应返回给回调函数"done()"

submitHandler: function (form) {
var data = $(form).serialize();
$.post( "test.php", data)
  .done(function( response ) {
     console.log(response);
  });
}

【讨论】:

    【解决方案2】:

    您可以通过多种方式做到这一点:-

    $.post("test.php", data, function( response ){  // you missed sending form data part here which i have added 
       console.log(response); 
    });
    

    $.post( "test.php", $(form).serialize()).done(function( data ) {
       console.log(data);
    });
    

    var data = $(form).serialize();
    $.ajax({
      type: "POST",
      url: url,
      data: data,
      success: function(response){
         console.log(response);
      }
    });
    

    注意:- 这不仅仅是结束。还有更多方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-24
      • 2017-03-06
      • 2014-06-24
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多