5

I am doing a ajax call from java script and Im trying to get json response from php, if I set dataType as JSON, ajax error block is getting executed if not success block is getting executed, without specifying dataType if I try to console.log the response in success block I am getting nothing

JS

let CurrentDate = Date();
console.log(CurrentDate);

jsonObject = {
   'TrackName' : 'Material Science',
    'TrackDesc' : 'Test Text Test Text Test Text Test Text Test Text Test Text Test Text ',
    'Timestamp' : CurrentDate
}
console.log(jsonObject);
$.ajax({
    type:'post',
    url:'../../../../PHP/adminScripts/addNewTrack.php',
    contentType: "application/json",
    data: {trackDetails:jsonObject},
    dataType: "json",
    success: function(response) {
        console.log('SUCCESS BLOCK');
        console.log(response);
    },
    error: function(response) {
        console.log('ERROR BLOCK');
        console.log(response);
    }
});

PHP

<?php
header('Content-type: application/json');
include('../connection.php');

if($_POST) {
$obj = $_POST['trackDetails'];

$TrackName = mysql_real_escape_string($obj['TrackName']);
$TrackDesc = mysql_real_escape_string($obj['TrackDesc']);
$TrackAdderID = 'Admin ';                  //$_SESSION["userID"];
$Timestamp = mysql_real_escape_string($obj['Timestamp']);

$response_array['status'] = 'status123';
echo json_encode($response_array);

please help me figure out how to get json response to an ajax call in PHP

3
  • 1
    Are you sure that you are calling the Correct URL '../../../../PHP/adminScripts/addNewTrack.php' Commented May 9, 2017 at 8:50
  • any console errors? Commented May 9, 2017 at 8:50
  • can u try to remove contentType in the ajax then have dataType : "json",encode:true, then remove the header in php Commented May 9, 2017 at 9:02

4 Answers 4

4
  • In your JS file, remove contentType: "application/json",
  • In your php file, check "include file url" and
  • Close if statement block properly

JS File:

let CurrentDate = Date();
jsonObject = {
   'TrackName' : 'Material Science',
    'TrackDesc' : 'Test Text Test Text Test Text Test Text Test Text Test Text Test Text ',
    'Timestamp' : CurrentDate
}
$.ajax({
    type:'post',
    url:'addNewTrack.php',
    data: {trackDetails:jsonObject},
    dataType: "json",
    success: function(response) {
        console.log('SUCCESS BLOCK');
        console.log(response);
    },
    error: function(response) {
        console.log('ERROR BLOCK');
        console.log(response);
    }
});

PHP File:

<?php
    header('Content-type: application/json');
    include('../connection.php');

    if($_POST) {
        $obj = $_POST['trackDetails'];

        $TrackName = mysql_real_escape_string($obj['TrackName']);
        $TrackDesc = mysql_real_escape_string($obj['TrackDesc']);
        $TrackAdderID = 'Admin ';                  //$_SESSION["userID"];
        $Timestamp = mysql_real_escape_string($obj['Timestamp']);

        $response_array['status'] = 'status123';
        echo json_encode($response_array);
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Remove contentType: "application/json" from your JS code, and in the PHP file close the braces properly, it will work fine.

Comments

0
<?php

$return_code = 1;// or 0
$message = 'Message';
$time = date("Y-m-d H:i:s");

echo '{"return_code":"'.$return_code.'","mesaj":"'.$message .'","t":"'.$time  .'"}';
?>

then javascript in ajax success

<script>
success: function (e) {
const json = JSON.parse(e);
const validJSON = !!json;
}
</script>

json.t for time, json.return_code

Comments

0

You can convert the PHP array in JSON format with json_encode() function and return as a response. Set dataType: 'JSON' when send AJAX request.

Comments

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.