-2

The success function does not give results. How do I solve this?

I have the following code

function fun()
{
  var list_target_id = 'year';
  $.ajax({
    url: '://localhost/htdocs/cscart_mutli_car/index.php?dispatch=drill.drill',
    dataType: 'json',
    success: function (data) {
      alert(data);
    },
    error: function (xhr, status, error) {
      alert(xhr.responseText);
    }
  });
}



if ($mode == 'drill')
{
  $id = 166;
  $arr = array('category_id' = > 167, 'category' = > 'computers');
  echojson_encode($arr);
  exit;
}

Here in this Ajax I get the success alert. But it does not provide any results. How do I solve this?

Aravindan K
  • 101
  • 3
  • 18
  • It's returned an object. What's wrong with that? What's in the object? – i-CONICA Jan 07 '15 at 10:39
  • `[Object object]` is when you get when you stringify a plain object. You are returning a JSON representation of a pain object. `alert` only takes a string as input. You are getting the expected behaviour. – Quentin Jan 07 '15 at 10:39
  • i need to get echo json_encode($arr); in success function – Soniya Kaliappan Jan 07 '15 at 10:41
  • Use console.log to see the properties of your object. As said, you're passing an object to a method that only accepts strings. – i-CONICA Jan 07 '15 at 10:41
  • You've got it! It's in an object which is correct, re-read all above comments. – i-CONICA Jan 07 '15 at 10:42

1 Answers1

1

The data which is returned in the success handler is an Object.The alert will alert the string representation of the Object i.e. data.toString() which is [Object object]

You can get required things from data by calling properties from data object like this.

alert(data.category_id);

and

alert(data.category);
Kelsadita
  • 1,038
  • 8
  • 21
  • Its gives only undefined – Soniya Kaliappan Jan 07 '15 at 10:48
  • 1
    Soniya, use console.log(data) to see the structure of your object. – i-CONICA Jan 07 '15 at 10:56
  • Are you looking in your console, or expecting an alert to pop up? – i-CONICA Jan 07 '15 at 10:59
  • i need to get category id and categoryname – Soniya Kaliappan Jan 07 '15 at 11:03
  • Are you looking in your console, or expecting an alert to pop up? – i-CONICA Jan 07 '15 at 11:22
  • I used this code JSON.stringify(data) . Its provides {"notifications":[],"text":"{\"category_id\":\"167\",\"category\":\"computers\"}"} – Soniya Kaliappan Jan 07 '15 at 11:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68335/discussion-between-soniya-kaliappan-and-i-conica). – Soniya Kaliappan Jan 07 '15 at 11:47
  • Soniya. We're at a critical point here, where I can't help you any more unless you answer the question. Don't just keep telling me what code you've used. You're refusing to acknowledge the existence of objects, even though in JavaScript everything is an object. You're going to have to get to grips with them. Are you, when running console.log(data), looking in your console for the output, or expecting it to alert you a string? Objects aren't strings, you want to see a representation of the object in a tree format, which console.log will show you. – i-CONICA Jan 07 '15 at 12:08