skip to Main Content

I had tried to use a simple Ajax request with JS and PHP. My code:

test.js

$(document).ready(function () {
    $.ajax({
        url: window.location.href + 'test.php',
        type: 'GET',
        data: { 'testing': 'hey' },
        success: function (data) {
            console.log(data);
        },
        error: function (xhr, desc, err) {
            console.log(xhr);
            console.log('Details: ' + desc + 'nError: ' + err);
        }
    });

});

test.php

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

$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";

$json = json_encode($myObj);

echo $json;

index.php

<?php
include('db.php');

// ...
?>
<script src="./webroot/test.js" type="text/javascript"></script>

However, it produced a 500 error. I have tested with both Ajax post and Ajax get and got the same result. However, if I didn’t use json_encode and just echo out instead, the error disappears and I could see the message on the console.

test2.php

<?php
include('../db.php');

echo "this thing will work even though I didn't need";

I have checked the php error_log, got nothing. Add “header(‘Access-Control-Allow-Origin: *’);”, still the same. Echo json_last_error(), no response to even see the error.

The console:
enter image description here

enter image description here

At this rate, I would be unable to use any JS-library.
Any help would be really appreciated.

Edit: the $ before json_encode() is actually a typo I wrote while asking question. My current code didn’t have it and still got 500.
Edit2: Another typo. My current code did close tag properly.

3

Answers


  1. Chosen as BEST ANSWER

    Thanks to @vivek_23 I have realized the problem was not from my code (since the code ran fine in localhost) but my remote server. The PHP in my remote server didn't support JSON.

    To anyone having the same problem: You can run

    var_dump(function_exists('json_encode'));
    

    and see if it returns true or false. You can also run phpinfo() to see if there is a section where it said "json support: enabled". Seeing JSON in "Module Authors" section doesn't mean anything.

    Since my remote server powered by CentOS, I downloaded a JSON package:

    yum install php-json.x86_64

    and it works.


  2. you getting error 500 because of wrong syntax in your php file
    test.php

    update the code of test.php with below code

     <?php
      include('../db.php');
        header('Content-Type: application/json');
        $myObj = array();
        $myObj['name'] = "John";
        $myObj['age'] = 30;
        $myObj['city'] = "New York";
        $json = json_encode($myObj);
        echo $json;
    

    and I don’t know that you are using some framework because you are not closing your PHP end tag end of file ?> make sure to clear this as well.

    Login or Signup to reply.
  3. PHP has some built-in functions to handle JSON.

    Objects in PHP can be converted into JSON by using the PHP function json_encode():

    include('../db.php');
    header('Content-Type: application/json');
    
    $myObj->name = "John";
    $myObj->age = 30;
    $myObj->city = "New York";
    
    $myJSON = json_encode($myObj);
    
    echo $myJSON;
    

    Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search