shibbir ahmed shibbir ahmed - 1 year ago 109
Javascript Question

why json return data is showing [] (third bracket) in ajax success method?

I am validating a form using jQuery / Ajax method.

Bellow is my jQuery code and I am passing

data type. But in
method it's return any data with third braket like : [my return data]

jQuery Code :

$("#filter_c_n").change(function() {
var data = $("#filter_c_n").val();
url : 'search_c_n.php',
method : 'POST',
data : {
key : data
datatype : 'json',
success : function(data) {

Here is my php file which contain bellow code :


$data = array();
$key = inputvalid($_POST['key']);
$data['key'] = $key;
echo json_encode($data);

What is showing is bellow :

enter image description here

Answer Source

If you just want the string, without wrapping it in an object, use

echo json_encode($key);

There's no need for the $data array.

However, returning an associative array allows for more flexible coding, as you can return multiple items. For instance, you can include a status key, so that the client can check whether the request was successful. With the way you're returning the data, you just need to change the Javascript code to do:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download