weixin_33688840 2016-06-19 21:55 采纳率: 0%
浏览 7

简化ajax对象

I have this function to write the inputs from a form to the database and i'm trying to simplify the data being passed by the ajax call to the php. Currently i have this function:

function writeDB(stage){
    var userData = $("#cacheDB").find("input").get();
    var ajaxData = []
    for (var n=0;n < userData.length; n++){
        item = {};
        item[userData[n].id] = userData[n].value;
        ajaxData.push(item);
    }

    $.ajax({
        url: "x.php",
        data: {ajaxData},
    });
}

Which sends out this object:

http://url.php?ajaxData[0][pageid]=1&ajaxData[1][input1]=John&ajaxData[2][input2]=Doe

I would like to send only the original data key, like:

http://url.php?[pageid]=1&[input1]=John&[input2]=Doe

Or

http://url.php?pageid=1&input1=John&input2=Doe

Is it posible? I've tried several methods and havn't found one suitable yet.

  • 写回答

1条回答 默认 最新

  • weixin_33711647 2016-06-19 22:12
    关注

    I would try to serialize the form instead of that for loop. It basically sends all input fields from your form as a JSON object to the server.

    $.post('server.php', $('#theForm').serialize())
    

    For larger form, HTTP POST method should be prefferd, you can send complicated object.

    Check this for more help: jQuery AJAX submit form

    Hope its what you need.

    评论

报告相同问题?

悬赏问题

  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))