Home » Jquery » javascript – Ajax success has became nested loop-Exceptionshub

javascript – Ajax success has became nested loop-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have used jquery to fetch JSON data. But it goes wrong. I have checked everything but did not find any solution. If I want to print any data it prints around 6-7 times. But It only should show one time.

Web Inspect to see the console

$(document).ready(function() {
  $('div').on("click", 'a', function() {
    var i = 0;
    var delivery_id = $(this).attr("id");

    $.ajax({
      url: "http://localhost:8888/dashboard/fetch_edit.php",
      method: "POST",
      data: { user_id: user_id },
      data: { tracking_id: tracking_id },
      data: { email: email },
      data: { phone: phone },
      data: { company_name: company_name },
      data: { com_email: com_email },
      data: { company_phone: company_phone },
      data: { company_address: company_address },
      data: { delivery_type: delivery_type },
      data: { packing: packing },
      data: { product_weight: product_weight },
      data: { preferred_time: preferred_time },
      data: { delivery_charge: delivery_charge },
      data: { customer_name: customer_name },
      data: { to_phone: to_phone },
      data: { to_address1: to_address1 },
      data: { to_zone: to_zone },
      data: { to_post_code: to_post_code },
      data: { u_status: u_status },
      data: { notes: notes },
      data: { delivery_id: delivery_id },
      dataType: "json",
      success: function(data) { //I have checked with chrome debugger. For this line loop is running.
        console.log(i);
        $('#delivery_id').val(delivery_id);
        console.log(delivery_id);
        $('#user_id').val(data.user_id);
        $('#tracking_id').val(data.tracking_id);
        $('#email').val(data.email);
        $('#phone').val(data.phone);
        $('#company_name').val(data.company_name);
        $('#com_email').val(data.com_email);
        $('#company_phone').val(data.company_phone);
        $('#company_address').val(data.company_address);
        $('#delivery_type').val(data.delivery_type);
        $('#packing').val(data.packing);
        $('#product_weight').val(data.product_weight);
        $('#preferred_time').val(data.preferred_time);
        $('#delivery_charge').val(data.delivery_charge);
        $('#customer_name').val(data.customer_name);
        $('#to_phone').val(data.to_phone);
        $('#to_address1').val(data.to_address1);
        $('#to_zone').val(data.to_zone);
        $('#to_post_code').val(data.to_post_code);
        $('#u_status option[value="' + data.u_status.toLowerCase() + '"]').prop('selected', true);
        $('#notes').val(data.notes);
        console.log(data.notes);
        console.log(JSON.stringify(data.products));
      }
    });
  });
});

This is the response of the AJAX request:

{
  "id": "2",
  "send_id": "10",
  "tracking_id": "TI-000000010",
  "user_id": "10",
  "username": "istiaqahmed",
  "email": "[email protected]",
  "phone": "0176430886",
  "company_name": "EVALY.COM.BD",
  "company_phone": "01747588386",
  "company_address": "Dhanmondi 28",
  "com_email": "[email protected]",
  "delivery_type": "Standard",
  "packing": "regular",
  "product_weight": "1",
  "preferred_time": "morning",
  "delivery_charge": "60",
  "customer_name": "Zenith Jhony",
  "to_address1": "75\/1 Jafrabad Pulpar Pabna House Goli",
  "to_phone": "01776065208",
  "to_zone": "Dhaka",
  "to_post_code": "1207",
  "date": "2020-01-09 23:26:10",
  "u_status": "Approve",
  "notes": "note",
  "products": [{
    "product_id": "1",
    "product_name": "shampoo",
    "product_quantity": "2",
    "product_price": "2333",
    "customer_send_id": "10"
  }, {
    "product_id": "2",
    "product_name": "assf",
    "product_quantity": "1",
    "product_price": "232",
    "customer_send_id": "10"
  }]
}
How to&Answer: