dongqiong8021 2014-02-05 15:44
浏览 127
已采纳

.serialize不能正确传递参数

I am sending a serialized form with $.post to a Controller PHP:

$.post('/Controller.php',
    {
        action: 'register',
      data: $('#regForm').serialize()                       
    },
  function(msg){

        if(parseInt(msg.status)==1)
        {
            window.location=msg.txt;
        }
        else if(parseInt(msg.status)==0)
        {
            error(1,msg.txt);
        }

        hideshow('loading',0);
    }, 
    "json"      
);  

I would now expect to be able to access the Form fields by $_POST['fieldname'] but INSTEAD I have a Get like parameter string in $_POST['data'] -.- What do I do wrong ?

  • 写回答

3条回答 默认 最新

  • douzhuo1858 2014-02-05 15:51
    关注

    It's sending the data just as you told it to. .serialize() creates a "query string", which you added into $_POST['data'].

    Instead of what you have, try this:

    var formData = $('#regForm').serialize();
    var extraFields = {action: 'register'};
    
    $.post('/Controller.php', $.param(extraFields)+'&'+formData, function(msg){
        if(parseInt(msg.status)==1)
        {
            window.location=msg.txt;
        }
        else if(parseInt(msg.status)==0)
        {
            error(1,msg.txt);
        }
    
        hideshow('loading',0);
    }, 'json');
    

    Now you should be able to get $_POST['action'] and $_POST['fieldname'].

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

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?