0

I am getting the string format instead of array format which is outputted from ajax

$.get('ajax/order_details.php?order_limit=true&order_limit_id=<?php echo $_GET['id']; ?>', function(data){
                                alert(data.toSource());

});

And this is the code which i am using it in php file (order_details.php)

if(isset($_GET['order_limit_id']) and isset($_GET['order_limit'])){
    $g_o = $mysql->query("SELECT * FROM ocreturn r, ocorder o WHERE o.order_id = '".$_GET['order_limit_id']."' and r.customer_id = o.customer_id");
    echo json_encode($g_o->rows);
}

And I am getting the output as

(new String("[{\"return_id\":\"129\",\"order_id\":\"126450\",\"parent_status\":\"0\"}]"))

I want output as array not string .

Can anyone give me the solution for this

2
  • what happens if you just call alert(data)? Commented Mar 5, 2013 at 7:44
  • it displays as like this "[{\"return_id\":\"129\",\"order_id\":\"126450\",\"parent_status\":\"0\"}]" but it is also string Commented Mar 5, 2013 at 7:50

2 Answers 2

1

You can instruct jQuery to parse the response as JSON by passing the string 'json' as the third parameter to the $.get function:

$.get('ajax/order_details.php?order_limit=true&order_limit_id=<?php echo $_GET['id']; ?>', function(data){
  alert(data.toSource());
}, 'json');

If you are wanting to use jQuery's lower-level $ajax function, you can use the following:

$.ajax({
  url: ajax/order_details.php,
  data: {order_limit: true, order_limit_id: <?php echo $_GET['id']; ?>},
  success: function (data) {
    alert(data);
  },
  dataType: 'json'
});

This effectively does the same thing as the call to $.get. Remember that the $.get method is just a shorthand for the $.ajax method. See the jQuery docs on $.get.

Sign up to request clarification or add additional context in comments.

1 Comment

It works . Also i want the solution for $.ajax(); . I am getting string as output for this also . can u give me the solution for this
0

you should use JSON , the server should return JSON array and the jquery should parse this json not string see:
http://php.net/manual/en/function.json-decode.php

and also the jquery should be

$.getJSON("URL",function(msg){
     alert("result : "+msg);
});

2 Comments

It works thanks. And also i have another doubt If i use the $.ajax(); I am getting output as string what i had it before .
Plz take care to accept the answer if it solves your problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.