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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵