R
R
rennamed_user1232021-09-21 13:12:11
AJAX
rennamed_user123, 2021-09-21 13:12:11

ajax problem?

I need to send an array to the server, for some reason [Object object] is sent, if you look through console.log, then the output is normal, but on the server it is already [Object object]

array_setting = {
      addresses: [
        {
          type: 'home',
          street1: $('.block-info #street1').text(),
          street2: $('.block-info #street2').text(),
          city: $('.block-info #city').text(),
          state: $('.block-info #state').text(),
          zip_code: $('.block-info #zipCode').text(),
          country: $('.block-info #country').text()
        }
      ],
      date_of_birth: $('.block-info #dateOfBirth').text(),
      emails: [
        {
          type: 'default',
          email: $('.block-info #email').text()
        }
      ],
      gender: {
        "id": $('.block-info #gender').attr('data-id'),
      },
      name: {
        first_name: $('.block-info #firstName').text(),
        last_name: $('.block-info #lastName').text(),
        preferred_name: ""
      },
      phones: [
        {
          type: 'emergency',
          area_code: $('.block-info #phoneEmergency').attr('data-code'),
          number: $('.block-info #phoneEmergency').attr('data-number'),
          extension: "",
        },
        {
          type: "home",
          area_code: $('.block-info #phoneEmergency').attr('data-code'),
          number: $('.block-info #phoneEmergency').attr('data-number'),
          extension: "",
        }
      ],
    };

    $.ajax({
      url: '<?php echo admin_url( "admin-ajax.php" ) ?>',
      type: 'POST',
      data: 'action=editAccount&settings=' + array_setting,
      success: function( data ) {
        console.log(data);
      }
    });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2021-09-21
@rennamed_user123

$.ajax({
  url: '<?php echo admin_url( "admin-ajax.php" ) ?>',
  type: 'POST',
  data: {
    action: 'editAccount',
    settings: array_setting,
  },
  success: function( data ) {
    console.log(data);
  }
});

R
Rsa97, 2021-09-21
@Rsa97

data: 'action=editAccount&settings=' + array_setting,

In this line you add the string with the object. According to the JS standard, this converts the object to the string '[object Object]'.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question