我对AJAX请求不熟悉,所以我相信我可能会犯一个简单的错误,但每当我想使用AJAX请求运行我的脚本时,我都会得到一个500 (Internal Server Error)
基本上我想要发生的是,如果用户按下#show-all
按钮,我将运行一个showall.php
脚本,该脚本从data.txt
文件读取任务并在HTML上打印任务网页。
非常感谢任何建议!
PS:第24行引用$.ajax({
,第27行引用console.log("Error: " + error);
Ajax请求
$("#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 .= "\n";
$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 \n in it
$arr[4] = str_replace("\n", "", $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>";
}
?>
答案 0 :(得分:1)
我认为这不是ajax问题。如果您访问http://localhost:8888/p2/showall.php
我认为您将收到相同的500错误。尝试检查你的服务器,如果它是一个php问题,创建一个html文件来返回你想要的相同内容,将更容易调试。