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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!