2

This is my json file after php encode values:

/database/database.php

[{"id":"1","title":"text","text":"ttexte","image":"dsgdsgs","User_id":"1"},{"id":"2","title":"titles","text":"sfsf","image":"safasfa","User_id":"1"}]

This is my Ajax code /js/database.js

$.ajax({
    url: "/database/database.php",
    type: "GET",
    dataType: 'json',
    crossDomain: "true",
    success: function(result) {
        if (result.type == false) {
            alert("Error occured:" + result.data);
            return false;
        }
        $.each(JSON.parse(result.data), function(index, obj) {
            $("#get-all").append(

                "<div class='span12'><div class='row'><div class='span8'><h4><strong><a href='#'>" + obj.title + "</div></a></strong></h4> </div></div>" +
                "<div class='row'><div class='span2'><a href='#' class='thumbnail'><img src='" + obj.id + "' alt=''></a></div><div class='span10'>" +
                "<p>" + obj.text + "    </div></p></div>")
            console.log(obj.text);
        });

    }
});

I'm in /blogs.php to retrieve values using call <script src="js/database.js"></script>

Console log: XHR finished loading: GET "/database/database.php".

I have already done debug and don't receive values.

Talha Awan
  • 4,070
  • 4
  • 21
  • 39
  • Possible duplicate of [jQuery AJAX Call to PHP Script with JSON Return](https://stackoverflow.com/questions/19155192/jquery-ajax-call-to-php-script-with-json-return) – Professor Allman Jul 25 '17 at 18:41
  • 1
    @ProfessorAllman i have already see that posts and dont find the answer, i have already done this call with Nodejs and works, with php don´t. –  Jul 25 '17 at 18:45
  • Did you verify that you are setting the correct header in php `header('Content-Type: application/json');` and it is getting returned to the browser? – Professor Allman Jul 25 '17 at 18:54
  • @ProfessorAllman when i use that the code transforms in html all... –  Jul 25 '17 at 19:33
  • Could you post the full source for database.php? be sure to redact any sensitive information – Professor Allman Jul 25 '17 at 19:42

1 Answers1

0

The result returned using AJAX is already JSON parsed, so the JSON.parse is not required and is the reason why it fails.

Just modify your code to avoid that and you should be fine.

$.ajax({
url: "/database/database.php",
type: "GET",
dataType: 'json',
success: function(result) {
    if (result.type == false) {
        alert("Error occured:" + result);
        return false;
    }
    $.each(result, function(index, obj) {
        $("#get-all").append(
            "<div class='span12'><div class='row'><div class='span8'><h4><strong><a href='#'>" + obj.title + "</div></a></strong></h4> </div></div>" +
            "<div class='row'><div class='span2'><a href='#' class='thumbnail'><img src='" + obj.id + "' alt=''></a></div><div class='span10'>" +
            "<p>" + obj.text + "    </div></p></div>")
        console.log(obj.text);
    });

}
});
HSR
  • 131
  • 8
  • Hello, @HSR dont works your script, i have tried and show the same message –  Jul 25 '17 at 19:04
  • I used test.php instead of /database/database.php, I have updated it now, can you confirm that you've changed that as well? – HSR Jul 25 '17 at 19:05
  • Oh, that's weird, I seem be getting the console output after the changes. http://imgur.com/rMydle3 – HSR Jul 25 '17 at 19:18
  • i´m dont getting that –  Jul 25 '17 at 19:20
  • Are you getting any errors in your console or is it just blank? – HSR Jul 25 '17 at 19:22