Markoh
Markoh

Reputation: 357

Javascript variable has random setting values

I am using ajax to get a variable from my controller, and everything is great until I push the returned data into another variable.

Here is my snippet of code

$(".event_selector").change(function() {

    $.ajax({
      url: "/get_customer_values",
      type: "GET",
      async:false,
      data: {event_selected: $(".event_selector").val()},
      success: function(data){
        var customer_values = {};
        for (var i in data) {
          //This alert has the correct values
          alert(i + " " + data[i]);
          customer_values[i] = data[i];
        }
      }
    });

    for(var i in customer_values) {
      //This is the alert that shows all the crazy values
      alert("customer_values " + i + " " + customer_values[i]);
    }

    add_customer_values_to_panel(customer_values)

  });

The first alert, using i and data[i] is perfect, with all the right values. The second alert using the customer_values hash has values that look like settings, such as:

KEY VALUE

click function click() { [native code] }

properties [object HTMLPropertiesCollection]

oncanplaythrough null

onchange null

onclick null

oncontextmenu null

ondblclick null

ondrag null

ondragend null

getAttributeNames function getAttributeNames() { [native code] }

getAttribute function getAttribute() { [native code] }

getAttributeNS function getAttributeNS() { [native code] }

It has at least 100 other values that look like this.

I tried moving the var customer_values initialization to outsite the ajax, but that doesn't work(nothing happens, and the first alert isn't even triggered).

What is going on? Where is that data coming from? Thank you.

Upvotes: 0

Views: 55

Answers (1)

Akshay
Akshay

Reputation: 805

your "customer_values" object is not global, it is defined inside ajax success function...

Upvotes: 2

Related Questions