简体   繁体   中英

js working in console but not from function

I'm calling the below function after the data from an ajax call is returned. It performs the first console.log() which contains the data from the ajax call. But the each() loop iterating through - response, does not console.log anything.

primaSaveOrder.prototype = {

     start: function(response){

        console.log(response)

        $j('#primaId').val('');
        $j('#dialog').closest('.ui-dialog-content').dialog('close');


        $j.each(response['billing'], function(i, val) {
          console.log(response['billing'][i])
        });
    },
 }

I don't think my code is too wrong though because if I manually create a variable in the console equal to the output of - console.log(response) and then run the -

  $j.each(response['billing'], function(i, val) {
          console.log(response['billing'][i])
        });

it works. What I would prefer to run is $j('#' + i).val(response['billing'][i]) instead of console.log(response['billing'][i] .

This also works if I do it directly in the browser but not in the file. I assume it's related but I can't figure out how to fix it.

this is an example of what i am iterating with some data changed for privacy

  {"billing":{"order-billing_address_firstname":"test","order-billing_address_lastname":"test","order-billing_address_street0":"6  test","order-billing_address_street1":"test","order-billing_address_city":"","order-billing_address_country_id":"GB","order-billing_address_region":"","order-billing_address_postcode":"16","order-billing_address_telephone":"","order-billing_address_vat_id":"","order-billing_address_prima_address_code":"0"},"shipping":{"order-shipping_address_firstname":"test","order-shipping_address_lastname":"test","order-shipping_address_street0":"6 test","order-shipping_address_street1":"test","order-shipping_address_city":"","order-shipping_address_country_id":"GB","order-shipping_address_region":"","order-shipping_address_postcode":"16","order-shipping_address_telephone":"","order-shipping_address_vat_id":"","order-shipping_address_prima_address_code":"0"}} 

May be the type of response is String. In that case convert the response into json using

response = JSON.parse(response);

before using response for iteration.

I don't say, it will help, but there are several things that should be checked for issue root:

  1. Check whether the elements exist and they are really unique (since ids are used). console.log(i, $j('#' + i).size()) may do the trick. I believe that there may be no elements to set values.
  2. Use val argument directly instead of referencing it through response['billing'][i] . Though I don't see how the response var may be overridden or hidden, but let's use the value directly since it's passed into callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM