dongxiusuo9881 2017-02-24 19:10
浏览 332
已采纳

简单的Ajax请求将数据从文本文件回显到HTML获取GET 500(内部服务器错误)

Problem

I'm new to AJAX request so I believe I might be making a simple mistake, but whenever I want to run my script using an AJAX request I get a 500 (Internal Server Error)

enter image description here enter image description here

Basically what I want to happen is if the user presses the #show-all button I will run a showall.php script that reads tasks from a data.txt file and prints the tasks in HTML on the webpage.

Any advice would be greatly appreciated!

PS: Line 24 refers to $.ajax({ and Line 27 refers to console.log("Error: " + error);

Code

Ajax request

 $("#show-all").on("click", function () {
    console.log("button pressed");
    $.ajax({
        url: '../p2/showall.php'
        , error: function (error) {
            console.log("Error: " + error);
        }
        , success: function (response) {
            console.log("Success: " + response);
        }
    });
});

showall.php

<?php 
    $filename = 'data.txt';
    include('file.php');
    include('add.php');
    $tasks = read_file($filename);
    foreach($tasks as $task){
        echo_task($task);
    }
?>

file.php

<?php
//Write task element to file
function write_file($filename, $task){
    $arr = array($task->title, $task->due_date, $task->priority, $task->course, $task->note);
    $line = implode("\t", $arr);
    $line .= "
";

    $file_pointer = fopen($filename, 'a' );
    if ( ! $file_pointer ) { echo( 'error' ); exit; }
    $error = fputs($file_pointer,$line);
    fclose($file_pointer);
}

//Read file $filename and return array of Tasks
function read_file($filename){
    $lines = file($filename);
    if(!$lines){
        print( 'error' ); 
        exit; 
    }

    $tasks = array();
    foreach($lines as $line){
        //Assume every entry should have notes
        $arr = explode("\t", $line);
        //Assume notes should not have 
 in it
        $arr[4] = str_replace("
", "", $arr[4]);
        $task = new Task($arr[0], $arr[1], $arr[2], $arr[3], $arr[4]);
        $tasks[] = $task;
    }

    return $tasks;
}
?>

add.php

<?php 
//Returns true if text field input isset & not empty string, otherwise returns false & echos issue to use
function validText($field){
    if(isset($_POST['add'])){
        if(isset($_POST[$field])){
            if($_POST[$field] !== ''){
                return true;
            }

            echo "<h3 class='error'>*Task $field cannot be empty</h3>";
            return false;
        }
        echo "<h3 class='error'>*Task $field must be set</h3>";
        return false;
    }

    return true;
}

//Return task from form elements
function task_from_form(){
    if(isset($_POST['add']) && isset($_POST['title']) && isset($_POST['note'])){     
        if($_POST['title'] !== '' && $_POST['note'] !== ''){
            $title = $_POST['title'];
            $note = $_POST['note'];
            $title_trim = trim($title);
            $note_trim = trim($note);
            $title_html = htmlentities($title_trim);
            $note_html = htmlentities($note_trim);

            $due_date = $_POST['due-date'];
            $priority = $_POST['priority'];
            $course = $_POST['course'];
            $course_space = str_replace("-", " ", $course);

            $task = new Task($title_html, $due_date, $priority, $course_space, $note_html);

            return $task; 
        }
    }
}

//Echo task
function echo_task($task){
    echo "<div class='task row'>
            <div class='task-title row'>
                <button type='button' class='checkbox col'><span class='icon-checkbox-box' aria-hidden='true'></span></button>
                <h1 class='col'>{$task->title}</h1>
                <div class='task-info'>
                    <h2 class='due-date col task-date'>{$task->due_date}</h2>
                    <button type='button' class='priority {$task->priority} col'><span class='icon-circle' aria-hidden='true'></span></button>
                </div>
            </div>
            <div class='task-details'>
                <div class='row'>
                    <h2 class='col'>{$task->course}</h2> </div>
                <div class='row'>
                    <p class='note col'>{$task->note}</p>
                </div>
            </div>
        </div>";
}

?>
  • 写回答

1条回答 默认 最新

  • dongsao8279 2017-02-24 19:26
    关注

    I believe this is not an ajax issue. If you visit http://localhost:8888/p2/showall.php I think you will get the same 500 error. Try to check your server, if its a php issue, create a html file to return same content as you want, will be easier to debug.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 IDEA 下 lombok 不生效,找不到get
  • ¥15 sas无法加载传输模块
  • ¥15 ABAQUS模拟钢管混凝土往复荷载,滞回曲线卸载荷载却比加载荷载大是为什么
  • ¥15 IOS设置了charels得代理,但是显示无互联网连接,无法打开chls.pro/ssl下载证书
  • ¥40 一维度流体仿真软件前端如何开发
  • ¥500 DELPHI环境安卓定位实时发送及回调消息展示
  • ¥15 易视腾is-E5-ngh怎么刷机,求各位们分享一下吧
  • ¥15 Androidstudio的程序,求各位帮帮我
  • ¥15 岩石声发射矩张量反演软件
  • ¥15 用Python完成一个任务
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部