我正在尝试从原始PHP(没有框架)制作一个非常简单的API。
当我在浏览器http://localhost/simpleAPI/countries.php
上输入时,它会输出我想要的数据。
但是当我使用和ajax调用时,我收到内部服务器错误响应。
<?php
require_once 'Database.php';
header("Content-Type: application/JSON");
$mysqli = Database::getInstance();
$data = [];
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT * FROM Country LIMIT 10")) {
while ($row = $result->fetch_object()){
$data[] = $row->Name;
}
/* free result set */
$result->close();
}
$res = response(200, 'ok', $data);
function response($status, $message , $data){
header("HTTP/1.1 $status $message"); <-- working Now (after adding $status)
$res['status'] = $status;
$res['message'] = $message;
$res['data'] = $data;
echo json_encode($res);
}
AJAX
var jqxhr = $.get( "countries.php", function(data) {
alert( data);
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
错误
jquery.min.js:4 GET http://localhost/simpleAPI/countries.php 500 (Internal Server Error)