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.

5
  • Possible duplicate of jQuery AJAX Call to PHP Script with JSON Return Commented Jul 25, 2017 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. Commented Jul 25, 2017 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? Commented Jul 25, 2017 at 18:54
  • @ProfessorAllman when i use that the code transforms in html all... Commented Jul 25, 2017 at 19:33
  • Could you post the full source for database.php? be sure to redact any sensitive information Commented Jul 25, 2017 at 19:42

1 Answer 1

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);
    });

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

5 Comments

Hello, @HSR dont works your script, i have tried and show the same message
I used test.php instead of /database/database.php, I have updated it now, can you confirm that you've changed that as well?
Oh, that's weird, I seem be getting the console output after the changes. imgur.com/rMydle3
i´m dont getting that
Are you getting any errors in your console or is it just blank?

Your Answer

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