Undefined Object Property JS

I am trying to access the properties of this object but i keep getting undefined. This is what i return from PHP json_encode($return, true);

i try to access results.message (undefined)
or results.status (undefined)

My jquery:

jQuery.ajax({

url: "order_sync.php",
method: 'post',
dataType: 'json',
data: {
  order: obj
},
error: function(){
    
},
success: function(results){


  if(results.status === '0'){


    $(order_button).closest('.error').show();

  } else {

$(order_button).closest('.success').show();

  }

}

})

Open in new window



This is the JS returned to me from PHP and an ajax call:

[  
   {  
      "status":"1",
      "message":"Some Message.",
      "order_id":"6336",
      "country":"United States",
      "customer_name":"Customer",
      "ship_method":"Fedex Ground (Fedex Ground)"
   }
]

Open in new window

Matthew BHackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Looking at your JS, your object is contained within an array, so you'd need to access the first element of the array before accessing the object - results[0]:

if(results[0].status === '0'){

It probably makes more sense to edit your PHP to make sure you just return just the object rather than the array containing the object.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matthew BHackAuthor Commented:
Thank you Chris.

I resolved this by creating an object in my PHP and return this instead of returning an array.

           $object = new stdClass();
            $object->status = '1';
            $object->message = 'message';
            $object->order_id = $orderId;
            $object->country = $orderCountry
            $object->customer_name = $orderDetails;
            $object->ship_method = $orderShippingDetails ;

$object_json = json_encode($object,true)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.