Ajax response is null on PHP correctly handled data

ValeMiri

I have a form that performs a POST call on a php file on the server; the request is processed correctly, but when I try to handle it, it is shown as a null value. Here my php fragment:

<?php
header('Access-Control-Allow-Origin: *');

require_once 'access.php';

//connection to db
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (mysqli_connect_errno()) { //verify connection
    echo "Error to connect to DBMS: ".mysqli_connect_error(); //notify error
    exit(); //do nothing else
}

//Take user team request
$teamchoice = $_POST['team'];

//retrieving all people of that team
$query = "SELECT * FROM pokecods WHERE team = '$teamchoice'";
$result = $mysqli->query($query);


if ($result->num_rows > 0) 
    echo json_encode($result);
else
    echo 'Oops, something went wrong';

$result->close();
$mysqli->close();
?>

The query performs correctly, because as result I have a json object (there's only one object that matches the query in the db), but when I try to handle this object, I have this result: result

here my jQuery code:

    $.post(post_url, post_data)
        .done( function(response) {
var json = JSON.parse(response);        
    console.log(json);
})
        .fail( function() {
            alert("The AJAX request failed!");
        });

what do I have to do in order to handle correctly my object?

Lovepreet Singh

You forgot fetch result after executing query. Fetch results using fetch_all and then pass to json_encode.

Updated code:

if ($result->num_rows > 0) {
    $records = $result->fetch_all(MYSQLI_ASSOC);
    echo json_encode($records);
} else {
    echo 'Oops, something went wrong';
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

send data to php with jquery AJAX and retrieve the response

From Dev

Cakephp: Ajax response is not correctly in "edit"

From Dev

My jQuery, AJAX call is getting a null response on a valid return of well-formatted JSON data from a PHP function

From Dev

Ajax response returning null

From Dev

Check if AJAX response data is empty/blank/null/undefined/0

From Dev

Ajax not parsing data correctly?

From Dev

Sending data to php page using ajax and get response and show in fields

From Dev

returning json data from php as response in javascript ajax

From Dev

print ajax response with php

From Dev

adding php response with ajax

From Dev

PHP array and Ajax response

From Dev

print ajax response with php

From Dev

AJAX not receiving PHP response

From Dev

Retrieving the PHP response in ajax

From Dev

JQuery Post to PHP Inserts data, but json response is null

From Dev

Ajax Data response data not alert

From Dev

Not Building jQuery ajax data correctly

From Dev

AJAX syntax error PHP response

From Dev

AJAX stacks response from PHP

From Dev

jQuery AJAX response not displaying PHP

From Dev

Returning a PHP response in a AJAX query

From Dev

Error in PHP file for Ajax response

From Dev

Get response from AJAX/PHP

From Dev

insert ajax response in data table

From Dev

AJAX $.post not showing response data

From Dev

JSON data ajax is not POST data correctly

From Dev

JQuery find() method on the ajax response returns null

From Dev

jQuery Ajax Json response - check if is null

From Dev

JSON returning null on response with jQuery $.ajax