Saleem
Saleem

Reputation: 1049

I am unable to save ajax data into a variable

I hava ajax call and I am getting the data back correctly. But i am unable to assign data to local variable. I am trying to assign data to item_price. I am getting data as 100.00 OR 115.25

Here is my ajax code

$.ajax({
              type: "get",
              url: "index.php?module=product&view=price",
              data: { code: v },
              dataType: "json",
              success: function(data) {
                    item_price = data;
                }

            });

Thank you Guys, async: false, worked for me.

Upvotes: 0

Views: 767

Answers (3)

Dipak Kumar Pusti
Dipak Kumar Pusti

Reputation: 1723

$.post('index.php', { module: product, view: price, code: v }, function(data)
{
    var item_price;
    item_price = data;
});

otherwise try this

$.post('index.php', { module: product, view: price, code: v }, function(data)
{
        $("#My_Div").html(data);
});

Upvotes: 0

shweta
shweta

Reputation: 8169

try this

var item_price;
$.ajax({
          type: "get",
          async: false, //If you need synchronous requests, set this option to false
          url: "index.php?module=product&view=price",
          data: { code: v },
          dataType: "json",
          success: function(data) {
                item_price = data;
            }

        });

Upvotes: 2

coolguy
coolguy

Reputation: 7954

$.ajax({
              type: "get",
              url: "index.php?module=product&view=price",
              data: { code: v },
              dataType: "json",
              success: function(data) { //console.log(data);/*get your price index and assign*/
                    item_price = data.your_price_index;
                }

            });

Upvotes: 0

Related Questions