doujiao1981 2015-02-23 05:21
浏览 50
已采纳

通过AJAX请求PHP脚本。 AJAX请求返回实际的PHP脚本

Requesting JSON from php script :

var channelList;

$(document).ready(function() {
    $.ajax({    
        url: 'channellookup.php',    
        dataType: 'json',    
        error: function(){console.log(arguments)},     
        success: function(data) {
            console.log(data.success);
            channelList = data;
        }  
    });
});

Now to the interesting part: The error message in the console reads like this:

Arguments { 0: XMLHttpRequest, 1: "parsererror", 2: "Invalid JSON: <?php
   header('Content-type: application/json');     // To ensure output json type.
   class MyDB extends SQLite3
   {

And so on. My whole PHP code is in that message. Something must go completely wrong here.

Here is my PHP in full

<?php

   header('Content-type: application/json');     // To ensure output json type.
   class MyDB extends SQLite3

   {
      function __construct()

      {
         $this->open('database_sqlite3.db');    
      }
   }

   $db = new MyDB();

   if(!$db){    
      echo $db->lastErrorMsg();    
   } else {    
      echo "Opened database successfully
";    
   }

   $sql =<<<EOF

      SELECT * from channels;

EOF;

   $ret = $db->query($sql);

   $channelList = array();

   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){    
      $channelList[] = $row;    
   }
   echo json_encode($channelList);
   $db->close();
?>

as you can see I do encode as json. As I said this works in the terminal. Please keep in mind that I do not want to use the json in my html yet so the page generating before the asynchronous request is completed isn't an issue yet.

Is the problem maybe that I am not doing this on a remote server but on local files? As I understand the Browser should be able to handle this case.

  • 写回答

2条回答 默认 最新

  • dooo61733 2015-02-23 05:32
    关注

    I'm quite sure that your PHP didn't compile at all. This is because the result that was returned, is in fact the body of your php file, indicating that no translation took place.

    You can check if your PHP instance is running by creating a file with the code:

    <?php phpinfo() ?>
    

    And use your browser to navigate to that page and see if there are any output. You want to check your PHP/Apache installation until the output is correct.

    As to why your webtool work, I cannot phantom any reason. It should fail in the same way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗