Ajax调用json_encode不起作用

时间:2017-12-08 16:04:10

标签: javascript php json ajax

我的问题是我的ajax调用和json响应。控制台告诉我,我的php文件没有返回json格式,但我不明白为什么。这是我的ajax功能:



        function showEspece(espece, categorie, object) 
  {
    $.ajax({           
      type : 'POST',                           
      url: 'getespece.php',                  
      data: {espece: espece, categorie: categorie },                       
      dataType: 'json',                     
      success: function(data)          
      {
        alert(data);
        var tableau = data;           
        $('#output').html(tableau); 
      },
      error: function(xhr, status, error) {
        console.log(xhr.responseText);
        console.log(error);
      }
    });
  }




这是ajax函数的php页面调用:

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

$requete = oci_parse($connect, "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '".$_POST['categorie']."' AND espece = '".$_POST['espece']."' ");

oci_execute($requete);

$test = oci_fetch_all($requete, $res);
$test1 = array();
$test1 = var_dump($res);


    echo json_encode($test1);

&GT;

我的问题是ajax函数总是出错,这是我在控制台中可以读到的内容:

    array(3) {
  ["NOMA"]=>
  array(3) {
    [0]=>
    string(6) "Chachi"
    [1]=>
    string(6) "Rafiki"
    [2]=>
    string(6) "Chakra"
  }
  ["SEXE"]=>
  array(3) {
    [0]=>
    string(1) "F"
    [1]=>
    string(1) "M"
    [2]=>
    string(1) "F"
  }
  ["DATENAISSANCE"]=>
  array(3) {
    [0]=>
    string(9) "05-MAY-15"
    [1]=>
    string(9) "07-JAN-15"
    [2]=>
    string(9) "17-SEP-17"
  }
}
null

SyntaxError: Unexpected token a in JSON at position 0
at parse (<anonymous>)
at Qb (jQuery.js:4)
at A (jQuery.js:4)
at XMLHttpRequest.<anonymous> (jQuery.js:4)

我已经过了一天,我不明白为什么它不起作用。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

您正在使用var_dump(),它不会返回任何内容,但会将变量的字符串表示形式发送到stdout。这正是你得到的。

您只需对oci_fetch_all查询的结果进行编码,然后将其作为json发送。不需要代码中的测试变量。

<?php

    header('Content-type: application/json');
    include("includes/connexionBD.php");

    $requete = oci_parse($connect, "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '".$_POST['categorie']."' AND espece = '".$_POST['espece']."' ");

    oci_execute($requete);

    oci_fetch_all($requete, $res);
    echo json_encode($res);

答案 1 :(得分:0)

你有一些错误,var_dump用于打印数据而不是为变量赋值,你的数据在$ res中不在$ test或$ test1中,请仔细检查你的查询中的数据,检查{{3} }

header('Content-type: application/json');
include("includes/connexionBD.php");

$requete = oci_parse(
    $connect,
    "SELECT nomA, sexe, datenaissance FROM Animal WHERE categorie = '" . $_POST['categorie'] . "' AND espece = '"
    . $_POST['espece'] . "' "
);

oci_execute($requete);

oci_fetch_all($requete, $res);

echo json_encode($res);

对于html结果,您可以尝试:

首先更改您的ajax请求

$.ajax({
  type    : 'POST',
  url     : 'getespece.php',
  data    : {espece: espece, categorie: categorie},
  dataType: 'html',    //You need to change from json to html
  success : function (data) {
    alert(data);
    var tableau = data;
    $('#output').html(tableau);
  },
  error   : function (xhr, status, error) {
    console.log(xhr.responseText);
    console.log(error);
  }
});

然后你的PHP代码

...
oci_execute($requete);

$test = oci_fetch_all($requete, $res);

header('Content-Type: text/html; charset=utf-8');
$table = '<table border="1">';
$rows = count($res);
for ($i = 0; $i < $rows; $i++){
    $cols = count($res[$i]);
    $table .= '<tr>';
    for ($j = 0; $j < $cols; $j++)
    {
        $table .= '<td>' . $res[$i][$j] . '</td>';
    }
    $table .= '</tr>';
}
$table .= '</table>';
echo $table;