Php 将文本文件中的数据回显到HTML GET 500的简单Ajax请求(内部服务器错误) 问题
我不熟悉AJAX请求,所以我相信我可能犯了一个简单的错误,但每当我想使用AJAX请求运行我的脚本时,就会出现500个内部服务器错误 基本上,我想做的是,如果用户按下showall按钮,我将运行showall.php脚本,从data.txt文件读取任务,并在网页上以HTML格式打印任务 任何建议都将不胜感激 PS:24行表示$.ajax{,27行表示console.logError:+error 密码 Ajax请求 showall.php file.php add.phpPhp 将文本文件中的数据回显到HTML GET 500的简单Ajax请求(内部服务器错误) 问题,php,jquery,ajax,Php,Jquery,Ajax,我不熟悉AJAX请求,所以我相信我可能犯了一个简单的错误,但每当我想使用AJAX请求运行我的脚本时,就会出现500个内部服务器错误 基本上,我想做的是,如果用户按下showall按钮,我将运行showall.php脚本,从data.txt文件读取任务,并在网页上以HTML格式打印任务 任何建议都将不胜感激 PS:24行表示$.ajax{,27行表示console.logError:+error 密码 Ajax请求 showall.php file.php add.php 我相信这不是ajax
我相信这不是ajax的问题http://localhost:8888/p2/showall.php 我想你会遇到同样的500错误。尝试检查你的服务器,如果是php问题,创建一个html文件以返回与你想要的内容相同的内容,这样会更容易调试。500=>如果你还没有检查日志,请检查日志。尝试转到提供500 e的链接错误,请检查它是否存在。也许您在ajax调用中给出的相对路径不起作用。@Fred ii-Right!谢谢!
$("#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);
}
});
});
<?php
$filename = 'data.txt';
include('file.php');
include('add.php');
$tasks = read_file($filename);
foreach($tasks as $task){
echo_task($task);
}
?>
<?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;
}
?>
<?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>";
}
?>