Jquery: How can I combine two json data objects and post them?

后端 未结 2 1633
旧巷少年郎
旧巷少年郎 2020-12-22 09:55

Following on this post, I have another problem - how can I combine two json data objects?

first json object,

{\"file\":{\"name\":\"1024x768.jpg\",\"t         


        
相关标签:
2条回答
  • 2020-12-22 10:19

    Use $.extend:

    $.post("process.php", $.extend(true, first, second) ,function(xml){
    
    });
    
    0 讨论(0)
  • 2020-12-22 10:26

    Looking at your first data object file is not an array as you are expecting it in the server side php code. It is a JSON object. Anyways you can try this to combine the data objects and post it.

    var data1 = {"file":{"name":"1024x768.jpg","type":"image\/jpeg","tmp_name":"C:\\wamp\\tmp\\php2C2E.tmp","error":0,"size":469159}};
    var data2 = {"title":"title","content":"bla bla blah"}
    
    var data = data1;
    data.title = data2.title;
    data.content = data2.content;
    
    $.post("process.php", data ,function(xml){
    
    });
    

    Alternatively you can use jQuery.extend method which merge the contents of two or more objects together into the first object.

    var data1 = {"file":{"name":"1024x768.jpg","type":"image\/jpeg","tmp_name":"C:\\wamp\\tmp\\php2C2E.tmp","error":0,"size":469159}};
    var data2 = {"title":"title","content":"bla bla blah"}
    
    $.post("process.php", $.extend({}, data1, data2) ,function(xml){
    
    });
    
    0 讨论(0)
提交回复
热议问题