weixin_33749242 2017-02-17 13:49 采纳率: 0%
浏览 18

jQuery Ajax参数问题

This is my code:

$('body').on('click', '.update_contact_profile', function (){
            var url = $("#ajaxUrl").val();
            var ids = $(this).closest("div").nextAll(".contact-extra-info").find(".contact-ids").attr("id");
            ids = ids.split("-")
            var contactId  = ids[0];
            var customerId = ids[1];
            var postDataUpdate = [];
            $(this).closest("div").nextAll(".update_elements").find(".value :input").each(function(i, itemVal){
                if ($(this).val()) {
                    postDataUpdate[''+$(this).attr('id')+''] = $(this).val();
                }
            });
            var request = $.ajax({
                url: url,
                method: "POST",
                data: {
                    id         : contactId,
                    contact    : postDataUpdate,
                    form_key   : FORM_KEY,
                    customerId : customerId
                }
            });

            request.success(function( text ) {  // replace the ajax_wrapper with the new one
                $(".ajax_wrapper").replaceWith(text);
                $("#contact_form").find('select').selectize();
            });
            request.fail(function( jqXHR, textStatus ) {
                alert( "Request failed: " + textStatus );
            });
        });

My problem is that this var postDataUpdate it didn't passed to ajax. On firebug the contact doesn't appear. If I do console.log(postDataUpdate) before my ajax request i got my array .

So any idea about this ?

  • 写回答

1条回答 默认 最新

  • weixin_33736048 2017-02-17 14:08
    关注

    postDataUpdate should be an object, instead of an array:

    [..]
    var postDataUpdate = {};
    $(this).closest("div").nextAll(".update_elements").find(".value :input").each(function(i, itemVal){
        if ($(this).val()) {
            postDataUpdate[''+$(this).attr('id')+''] = $(this).val();
        }
    });
    [..]
    

    Check this snippet:

    var asArray = [];
    asArray[1] = "foo";
    asArray["foo"] = "bar";
    console.log("asArray:");
    console.log(asArray);
    
    var asObject = {};
    asObject[1] = "foo";
    asObject["foo"] = "bar";
    console.log("asObject:");
    console.log(asObject);

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码