简体   繁体   中英

How to receive JSON data from silex controller in AJAX

When I'm trying to receive JSON data from PHP file in AJAX everything works fine:

Test.php

<?php
header("Content-Type: application/json; charset=UTF-8");
$pdo = new PDO('mysql:host=localhost;dbname=database', 'root', '');
$stmt = $pdo->prepare("SELECT id, name FROM table");
$stmt->execute();
$stmt = $stmt->fetchAll(PDO::FETCH_CLASS);

echo json_encode($stmt);
?>

main.js

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myObj = JSON.parse(this.responseText);
      console.log(myObj);
      document.getElementById("par").innerHTML = myObj[4].id + " " + myObj[4].tag1;
    }
};
xmlhttp.open("GET", "http://localhost/project/app/models/Test.php", true);
xmlhttp.send();

But when I'm trying to receive JSON from silex controller:

Ajax.php

namespace AI\models;
use Silex\Application;
use Silex\Api\ControllerProviderInterface;

class Ajax implements ControllerProviderInterface
{
  public function connect(Application $app){
    $controllers = $app['controllers_factory'];
    $controllers->get('/', 'AI\models\Ajax::home');
    return $controllers;
  }

  public function home(Application $app){
    header("Content-Type: application/json; charset=UTF-8");
    $result  = $app['db']->prepare("SELECT id, name FROM table");
    $result->execute();
    $result = $result->fetchAll(\PDO::FETCH_CLASS, \AI\models\Ajax::class);
    echo json_encode($result);

    return $app['twig']->render('ajax.twig');
  }

}

main.js

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myObj = JSON.parse(this.responseText);
      console.log(myObj);
      document.getElementById("par").innerHTML = myObj[4].id;
    }
};
xmlhttp.open("GET", "http://localhost/project/app/models/Ajax.php", true);
xmlhttp.send();

I get an error in the console:

Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.xmlhttp.onreadystatechange (main.js:4)
xmlhttp.onreadystatechange @ main.js:4

I don't have any idea why there is a problem when I trying to receive JSON from a class.

If you are returning json you should use the JsonResponse Object . Also you definitely don't want to render a template as you currently have, you just want json data, why render a template?

You could follow the docs previously linked but, fortunately Silex has a helper method for returning json data and you don't even need to use the JsonResponse (directly):

<?php

public function home(Application $app){

  $result  = $app['db']->prepare("SELECT id, name FROM table");
  $result->execute();
  $result = $result->fetchAll(\PDO::FETCH_CLASS, \AI\models\Ajax::class);

  return $app->json($result);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM