Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. JSON response from PHP for ajax

JSON response from PHP for ajax

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpdatabasejavascriptphpcom
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    charlieko
    wrote on last edited by
    #1

    I'm trying to create a php file that always returns a json as response. The problem is, it looks like it's returning a valid JSON, but jquery.ajax always says "invalid json." I copied and pasted the response and stuck it in jsonlint.com and it says it's valid json. Can anyone point out what I'm doing wrong here? Why can't jquery (and other json parser) parse this even it's a valid json?

    header('Content-type: application/json');

    include("database.php");
    require_once "../lib/JSON.php";

    $json = new Services_JSON();

    $method = strtolower($_SERVER['REQUEST_METHOD']);

    $value = array();

    // Open the database connection first, which will be used later.

    $DB = new DBConnect();

    $DB->NewConnection("localhost", "db_name", "db_role", "db_pw" );

    // assume method is get here
    if ($method == "get"){
    $value = getEmployees();
    }

    else if ($method == "post"){
    }

    else if ($method == "put"){
    }

    else {
    echo "Error - unknown http request method";
    }

    // tried without callback stuff but didn't work either
    echo $_GET['callback'] . '(' . $json->encode($value) . ');';

    J N 2 Replies Last reply
    0
    • C charlieko

      I'm trying to create a php file that always returns a json as response. The problem is, it looks like it's returning a valid JSON, but jquery.ajax always says "invalid json." I copied and pasted the response and stuck it in jsonlint.com and it says it's valid json. Can anyone point out what I'm doing wrong here? Why can't jquery (and other json parser) parse this even it's a valid json?

      header('Content-type: application/json');

      include("database.php");
      require_once "../lib/JSON.php";

      $json = new Services_JSON();

      $method = strtolower($_SERVER['REQUEST_METHOD']);

      $value = array();

      // Open the database connection first, which will be used later.

      $DB = new DBConnect();

      $DB->NewConnection("localhost", "db_name", "db_role", "db_pw" );

      // assume method is get here
      if ($method == "get"){
      $value = getEmployees();
      }

      else if ($method == "post"){
      }

      else if ($method == "put"){
      }

      else {
      echo "Error - unknown http request method";
      }

      // tried without callback stuff but didn't work either
      echo $_GET['callback'] . '(' . $json->encode($value) . ');';

      J Offline
      J Offline
      jaypatel512
      wrote on last edited by
      #2

      It seems that everything is fine. Probably, the code segment is going on the last else statement, and so, it prints an invalid Json line there. Try debugging that particular location, and you probably would be able to find it.

      1 Reply Last reply
      0
      • C charlieko

        I'm trying to create a php file that always returns a json as response. The problem is, it looks like it's returning a valid JSON, but jquery.ajax always says "invalid json." I copied and pasted the response and stuck it in jsonlint.com and it says it's valid json. Can anyone point out what I'm doing wrong here? Why can't jquery (and other json parser) parse this even it's a valid json?

        header('Content-type: application/json');

        include("database.php");
        require_once "../lib/JSON.php";

        $json = new Services_JSON();

        $method = strtolower($_SERVER['REQUEST_METHOD']);

        $value = array();

        // Open the database connection first, which will be used later.

        $DB = new DBConnect();

        $DB->NewConnection("localhost", "db_name", "db_role", "db_pw" );

        // assume method is get here
        if ($method == "get"){
        $value = getEmployees();
        }

        else if ($method == "post"){
        }

        else if ($method == "put"){
        }

        else {
        echo "Error - unknown http request method";
        }

        // tried without callback stuff but didn't work either
        echo $_GET['callback'] . '(' . $json->encode($value) . ');';

        N Offline
        N Offline
        nickmaroulis
        wrote on last edited by
        #3

        what is the response? what does firebug say?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups