0

I'm trying to merge two objects I receive as JSON via Ajax, but I can not access the variable and declaring it global. What am I doing wrong?

var parametroswatcher = {
        // asinid: $('#rate').attr('data-id'),
        asinid: GetURLParameter('asin'),
        mod: '0'
    };

    var post = $.post("../../likes/like.php", parametros, 'json');
    post.done(function( data ) {
    postdata = jQuery.parseJSON(data);

    });
    var postwatch = $.post("../../watcher/watch.php", parametroswatcher, 'json');
    postwatch.done(function( data ) {
    postwatchdata = jQuery.parseJSON(data);

    });
    var postmerge = $.extend(postdata,postwatchdata);

    console.log(postmerge);

The answer of postdata = jQuery.parseJSON(data) should be: {"resplike":"needlogin"}.

And the answer of postwatchdata = jQuery.parseJSON(data) should be: {"respwatch":"needlogin"}.

But to access the console, instead of getting postdata and postwatchdata merged, I get an empty object.

Object {} product.js:61

Edit: I want when post and postwatch done, use data in product function.

The answer of postdata = jQuery.parseJSON(data) should be: {"resplike":"needlogin"}.

And the answer of postwatchdata = jQuery.parseJSON(data) should be: {"respwatch":"needlogin"}.

 function product(data){       

        var obj = jQuery.parseJSON(data);

        if (obj.resplike=='like'){
        var respuesta = 'No te gusta';
        }
        else if(obj.resplike=='dislike'){
        var respuesta = 'Te gusta';....blabla

I want to get in obj: {"resplike":"needlogin", "respwatch":"needlogin"}

treska
  • 21
  • 7
  • 1
    `postmerge` is being set before your __asynchronous__ calls have returned. – Evan Davis Dec 03 '13 at 15:53
  • Even though you are receiving JSON, the question actually has nothing to do with JSON. It's about how to merge *JavaScript objects* and handle multiple Ajax calls. – Felix Kling Dec 03 '13 at 15:55

1 Answers1

2

You cannot handle the result of an asynchronous call like that. All operations done on a async function call must be done within the callbacks of that async method. read more about this in answer

var parametroswatcher = {
    // asinid: $('#rate').attr('data-id'),
    asinid: GetURLParameter('asin'),
    mod: '0'
};

var post = $.post("../../likes/like.php", parametros, 'json');
var postwatch = $.post("../../watcher/watch.php", parametroswatcher, 'json');

$.when(post, postwatch).then(function(arg1, arg2){
    var postmerge = $.extend(arg1[0], arg2[0]);
    console.log(postmerge);
})

Also since you need to wait for the responses from two different requests you can use $.when() which will back the success handlers once all the passed promises are resolved.

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • I think you mean `arg2[0]`. And please add an explanation, don't just post code. – Felix Kling Dec 03 '13 at 15:54
  • @FelixKling I'm editing the answer... adding a link back to one of your famous answers – Arun P Johny Dec 03 '13 at 15:57
  • @ArunPJohny Object {0: "{", 1: """, 2: "r", 3: "e", 4: "s", 5: "p", 6: "w", 7: "a", 8: "t", 9: "c", 10: "h", 11: """, 12: ":", 13: """, 14: "n", 15: "e", 16: "e", 17: "d", 18: "l", 19: "o", 20: "g", 21: "i", 22: "n", 23: """, 24: "}", 25: " ", 26: " ", 27: " ", 28: " ", 29: " ", 30: " ", 31: " ", 32: " ", repeat: function} – treska Dec 03 '13 at 16:17
  • @treska is it what you are looking for – Arun P Johny Dec 03 '13 at 16:19
  • @treska can you print the values for `arg1[0]` and `arg2[0]` – Arun P Johny Dec 03 '13 at 16:19
  • @treska in that case try `var postmerge = $.extend(jQuery.parseJSON(arg1[0]), jQuery.parseJSON(arg2[0]));` – Arun P Johny Dec 03 '13 at 16:35
  • @treska: this is the correct answer, you might have to tweak it a little so that the JSON is parsed correctly. But overall it's the right approach and should help you solve the problem. – Felix Kling Dec 03 '13 at 16:57