0

I'm trying to get to my data that came from php using:

echo json_encode($rows);

when I comment out datatype: 'json' I can see normally encoded json string, but when I use it alert shows me array of objects. When I try to get the length of that object array I get 1, but it should be over 20 items in there. I tried doing search posts on here and tried different things iterating through json and none of that works for me for some reason. And I am using

mysqli_set_charset($connect, "utf8");

before my select statement on php side. here is my ajax call:

function search() {
    $.ajax(
    {
      url: "getFiles.php",
      dataType: 'json',
      type: "POST",
      data: {Filter: $("#txtSearch").val()},
      cache: false,
      async: false,
      success: function(data)
      {
        var jsonData = JSON.parse(data);
        alert(data);
        $.each(data, function(index, val)
          {
            alert(val.iddocuments);
            alert(val.docName);
            alert(val.fileName);
            alert(val.fl1);  
            alert(val.dateModified);   
          });   
      }
    });         
  }  
9
  • can show response. Commented Jan 31, 2017 at 16:58
  • 1
    Can you show us what echo json_encode($rows); gives you Commented Jan 31, 2017 at 16:58
  • 1
    if you use dataType: 'json', then the data variable should be already parsed as an JavaScript array, so you don't need to parse it again with JSON.parse. Commented Jan 31, 2017 at 17:05
  • 1
    add dataType: 'json', then put a console.log(data); statement in success function and tell me what it is shown in console tab, in Firebug Commented Jan 31, 2017 at 17:08
  • 1
    Let us continue this discussion in chat. Commented Jan 31, 2017 at 17:09

2 Answers 2

1

The problem could be with the structure of the array that you serialize to json format. Check, with var_dump($rows) in PHP to see the exact structure and this structure should match the structure used on the client (in the JavaScript code).

On the client side you could do some debugging to see exactly the structure of what you are receiving, for example with a call to console.log(data), in the success function. In any situation, on the server side, you must add the header header('Content-Type: application/json'); and on the client side add dataType: 'json', so that jQuery knows how to parse the data and make available to you as an JavaScript array.

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

Comments

1

The problem lies on the PHP side with your use of json_encode():

http://php.net/manual/en/function.json-encode.php

Referencing the function definition: "string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )", the following:

<?php
echo "Associative array always output as object: ", json_encode($d), "\n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
?>

will output this:

Associative array always output as object: {"foo":"bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}

JSON_FORCE_OBJECT (integer) Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0.

A similar, "shorthand" version would be:

json_encode($d, true);

It's also worth mentioning that you should be setting the correct output headers before echoing your output data:

header('Content-Type: application/json');

Ultimately, if you're using PHP 5.4 or newer (which you should be), implementing the JsonSerializable Interface is the best option, as it allows for much more flexible representation of your data from a JSON standpoint.

2 Comments

where do you post that code header('Content-Type: application/json'); is it inside index.php?
You would place that before echo json_encode($rows, true);

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.