Ajax does not receive data in json

Asked

Viewed 107 times

1

Personal I created a function with Ajax that should return the posts that are in the database. Ajax requires a PHP script that responds with a JSON but I get the following message in the browser console

Synchronous xmlhttprequest should not be used on the main thread due to to its detrimental effects on the user experience. For more information http://xhr.spec.whatwg.org/

Follow the front-end code:

$(document).ready(function () {
    var count = 0;
    var value = 4;
    function getOtherPubli() {
        var stringUrl = $(location).attr('href').split("/");
        var type = stringUrl[2];
        var id = stringUrl[3];
        $.ajax({
            method: "POST",
            url: "/sys/etc/other-publi.php",
            data: {noPost: id, q: value, b: count, type: type}

        }).done(function (answer) {
            if (answer.status == 'ok') {
                $('contant-others-publi').html(answer.string);
                count += value;
            } else {
                console.log("System (other post) error. Code: " + answer.status);
            }


        }).fail(function (jqXHR, textStatus) {
            console.log("System (other post) error. Request failed: " + textStatus);

        });
    }
    getOtherPubli();
});

Code in PHP:

<?php

include_once 'sys/lib.php';
include_once 'sys/db/base0.php';

$id = [];
$tl = [];
$cp = [];
$string = '';
$res = ['status'=>null,'string'=>null];

if (isAjax()) {
    $getPost = ["nopost" => (int) $_POST['noPost'], "q" => (int) $_POST['q'], "b" => (int) $_POST['b'], "type" => clrSqli($_GET['type'])];
    $db = connectdb();
    $get = $db->prepare("SELECT id,title,coverofpost FROM `posts` WHERE id <> :notPost AND type = :type ORDER BY `date` DESC LIMIT :begin, :end");
    $get->bindValue(":notPost", $getPost['nopost'], PDO::PARAM_INT);
    $get->bindValue(":begin", $getPost['q'], PDO::PARAM_INT);
    $get->bindValue(":end", $getPost['b'], PDO::PARAM_INT);
    $get->bindValue(":type", $getPost['type'], PDO::PARAM_STR);
    $get->execute();
    if ($get->rowCount() > 0) {
        while ($data = $get->fethc(PDO::FETCH_ASSOC)) {
            array_push($id, $data['id']);
            array_push($tl, $data['title']);
            array_push($cp, $data['coverofpost']);
        }
        $length = count($id);
        for ($i = 0;$i < $length;$i ++) {
            $string .= '<div class="other-publi" data-id="'.$id[$i].'">
                        <div class="limit-img-other-publi">
                            <img class="img-other-post" src="'.$cp[$i].'" alt="FOTO"/>
                        </div>
                        <div class="title-other-publi">
                            <span>'.$tl[$i].'</span>
                        </div>
                     </div>';
        }
        $res['status'] = 'ok';
        $res['string'] = $string;
    } else {
        $res['status'] = 404;
    }
} else {
    $res['status'] = 403;
}
echo json_encode($res,true);
  • This error has nothing to do with the code you posted. Do you have any more ajax calls? or is something missing that you didn’t put in the question? This error that gives tells in which line the problem is?

  • This error is displayed with reference to "jquery.min.js:4:16013". This is simply an ajax request for php that should resume json. Even I just checked here the object and it does not exist returns "Undefined".

  • First, I suggest you take a request test for your api using a Postman of life, just to test what returns in the request.

  • Does this ajax no longer have configuration parameters? you don’t have one ajaxSetup somewhere?

  • Sorry for the inconvenience

  • As you had said Sergio the error message has nothing to do with the code. And the fact that json is undefined is because the parameters being passed are wrong. When consulted in the database returns zero Rows. That’s settled.

  • Okay, good you found the problem. I closed the question because the problem had nothing to do with the question here. See you soon.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.