user1778459
user1778459

Reputation: 115

very simple ajax request returns [object Object]

Here is the javascript part of the code;

$.ajax
({
  type: 'POST',
  url: location.href,
  data: {
  'uploaded_data' : 'uploaded_data',
  },
  dataType: 'text',
  success: function(message) {},
  complete: function(message) 
  {
     alert(message);
  }
});

And here is the php part;

if(isset($_POST["uploaded_data"]))
{
    $text="test text";
    echo $text;
    exit();
}

For some reason alert message shows [object Object] message instead of "test text". And the weird thing is if I try it like this;

alert(JSON.stringify(message));

it alerts this message;

{"readyState":"4", "responseText":"test text","status":200,"statusText":"OK"}

Upvotes: 2

Views: 2648

Answers (2)

M Khalid Junaid
M Khalid Junaid

Reputation: 64496

Try this one message.responseText

$.ajax
({
  type: 'POST',
  url: location.href,
  data: {
  'uploaded_data' : 'uploaded_data',
  },
  dataType: 'text',
  success: function(message) {},
  complete: function(message) 
  {
     alert(message.responseText);
  }
});

Upvotes: 3

Alex Funk
Alex Funk

Reputation: 435

Change your complete function to this to get the message:

complete: function(data) {
    alert(data.responseText)
}

Upvotes: 1

Related Questions