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条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大