0

I have what I hope is a successful post to my client's json API (in that it correctly sends data) but I'm having a very hard time grabbing that data and using it as a variable to update my site...

    $.post(takeurl, function(data) {            
        var newPrice = json.data.newPrice;
        alert('hi' );
    }, "json");

    $(this).parents("tr").find(".large-detail").html(newPrice);

    return false;
});

Fiddle

Basically, in this example I want to update large-detail with the newPrice data from my API call, which is returned like this:

{"data":{"newPrice":80,"lockedInPrice":47},"errors":[],"success":true,"code":200}

Ideas? I just can't get newPrice through. I'm hoping I made a stupid syntax error, even if it'll make me feel like an idiot.

Zachary Kniebel
  • 4,686
  • 3
  • 29
  • 53
dilettante
  • 381
  • 1
  • 4
  • 19

2 Answers2

0

You need to update it in the callback function:

    var $self = $(this);

    $.getJSON(takeurl, function(data) {            
        var newPrice = data.data.newPrice;
        $self.parents("tr").find(".large-detail").html(newPrice);
    });

    return false;
});

Where you have it now it executes before the ajax function returns and outside of the scope of your newPrice variable.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • Hmm, I tried it there and it's not working... doesn't return anything. Can you tell if my .post call is written correctly? (I should have mentioned this is tabular data, and I stripped the tables out of the fiddle to trim it) – dilettante Apr 11 '13 at 17:22
  • @dilettante Do you need to use POST or can you use GET? If you can use GET than you can use `getJSON` like in my edited answer. – Paul Apr 11 '13 at 17:25
  • Thanks! I just tried that and am still getting nothing. Oddly, I just added console.log(newPrice); after declaring the variable and it's not appearing. So I changed it to console.log('hi); and nothing. Is it possible it's not calling, even if my console window shows the data being returned? – dilettante Apr 11 '13 at 17:31
  • @dilettante Do you have `json.data.newPrice` or `data.data.newPrice`? Try doing just `console.log(data);` – Paul Apr 11 '13 at 17:32
  • @dilettante Does `console.log(data)` show you anything useful? – Paul Apr 11 '13 at 17:34
  • It doesn't show anything... Nothing in my console except the response from the server. – dilettante Apr 11 '13 at 17:35
  • @dilettante Hmm.. Strange. What if right before the `var newPrice` line you put `console.log('data', data);` – Paul Apr 11 '13 at 17:38
  • Not a thing! If I put a console.log('hghjkh') before the callback, it appears. Must be an error in my call? – dilettante Apr 11 '13 at 17:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28051/discussion-between-dilettante-and-paulpro) – dilettante Apr 11 '13 at 17:42
0

The problem was due to an error in the JSON returned, which was prefixed with <pre> and </pre> code for testing by other engineers, but which returned a parseerror when I tried to work with it. Thanks @paulpro.

dilettante
  • 381
  • 1
  • 4
  • 19