javascript - Ajax XHR 加载失败,内部服务器错误 500
问题描述
非常感谢帮助。已经检查过与此标题类似的其他帖子,但不够具体。
我正在尝试在运行 DSM 6.2.3-25426 Update 2 的 Synology DS415play 上实现 php 脚本的 ajax 调用。我遵循此处给出的指导:https ://www.digitalocean.com/community/tutorials/submitting- ajax-forms-with-jquery,使用三个文件:index.html、form.js 和 process.php
但是,form.js 中的 ajax 调用失败并显示以下控制台消息:
VM322:1 POST http://192.168.7.20/~user/process.php 500 (Internal Server Error)
VM322:1 XHR failed loading: POST "http://192.168.7.20/~user/process.php"
网络日志显示以下内容:
Request URL: http://192.168.7.20/~user/process.php
Request Method: POST
Status Code: 500 Internal Server Error
Remote Address: 192.168.7.20:80
Referrer Policy: strict-origin-when-cross-origin
**Response Headers**
Accept-Ranges: bytes
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 1696
Content-Type: text/html
Date: Wed, 04 Nov 2020 23:44:21 GMT
ETag: "cfa-5ae3c755f3d80"
Keep-Alive: timeout=20
Last-Modified: Tue, 01 Sep 2020 08:39:34 GMT
Server: nginx
Vary: Accept-Encoding
**Request Headers**
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 5
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Cookie: stay_login=0; id=c6OiV4GQDLu86y7E3yfyJTNHdambIB1thxRyeiDnDjgBrXSdBh25Lw2pbbJDQo1WGppdrfrFrm208MiUZ0HfCw; smid=WFXk4Mx31ghEHii0PVWGAsbNsHnd3uBl-toxQM3_VofCuY-KKq5EdnvNq6V7PojMcNxbWVUwQ78s7DKHui-2rA
DNT: 1
Host: 192.168.7.20
Origin: http://192.168.7.20
Pragma: no-cache
Referer: http://192.168.7.20/~user/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
X-Requested-With: XMLHttpRequest
Form Dataview sourceview URL encoded
三个源文件都在这里(都存储在 user/www 文件夹中):
索引.html
<!doctype html>
<html>
<head>
<title>Look I'm AJAXing!</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> <!-- load bootstrap via CDN -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <!-- load jquery via CDN -->
<script src="form.js"></script> <!-- load our javascript file -->
</head>
<body>
<div class="col-sm-6 col-sm-offset-3">
<h1>Processing an AJAX Form</h1>
<!-- OUR FORM -->
<form action="process.php" method="POST">
<!-- NAME -->
<div id="name-group" class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" placeholder="Henry Pym">
<!-- errors will go here -->
</div>
<button type="submit" class="btn btn-success">Submit <span class="fa fa-arrow-right"></span></button>
</form>
</div>
</body>
</html>
表单.js
$(document).ready(function() {
// process the form
$('form').submit(function(event) {
// get the form data
var formData = {
'name' : $('input[name=name]').val()
};
// process the form
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'process.php', // the url where we want to POST
data : formData, // our data object
dataType : 'json', // what type of data do we expect back from the server
encode : true
})
// using the done promise callback
.done(function(data) {
// here we will handle errors and validation messages
if ( ! data.success) {
// handle errors for name ---------------
if (data.errors.name) {
$('#name-group').addClass('has-error'); // add the error class to show red input
$('#name-group').append('<div class="help-block">' + data.errors.name + '</div>'); // add the actual error message under our input
}
} else {
// ALL GOOD! just show the success message!
$('form').html('<div class="alert alert-success">' + data.message + '</div>');
}
})
// using the fail promise callback
.fail(function(data) {
//Server failed to respond - Show an error message
$('form').html('<div class="alert alert-danger">Could not reach server, please try again later.</div>');
});
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
});
});
进程.php
<?php
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
// validate the variable
if (empty($_POST['name']))
$errors['name'] = 'Name is required.';
// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}
// return all our data to an AJAX call
echo json_encode($data);
知道为什么我会收到内部服务器错误吗?
亲切的问候,安德鲁
解决方案
推荐阅读
- javascript - Javascript 数组给出的长度不正确,输出不一致
- sql - 选择要在查询或表单中显示为列的附加数据/值
- powershell - Get-ChildItem Cert:\(本地执行)与 Invoke-Command 结果差异混淆?
- javascript - Mapping and filtering a nested object
- asp.net - Entity Framework Core Join 标识 AspNetUser 表。自定义表/实体的 AspNetUser Id
- javascript - Gatsby/GraphQL 在代码中的行为与 GraphiQL 不同
- facebook - 是否有任何 API 可以使用电话号码获取 Facebook 个人资料 URL?
- android - 如何设置 KeyboardAvoidingView 键盘垂直偏移背景颜色?
- python - 下一页 Selenium/BeautfulSoup 中用于抓取电子商务网站的迭代
- java - 如何使用 JNI 将 Java Vector 转换为 std::vector