Javascript Ajax XHR加载失败,内部服务器错误为500
非常感谢您的帮助。已经检查过与此标题类似的其他帖子,但不够具体 我试图在运行DSM 6.2.3-25426 Update 2的Synology DS415play上实现php脚本的ajax调用。我遵循这里给出的指导:使用三个文件:index.html、form.js和process.php 但是,form.js中的ajax调用失败,出现以下控制台消息:Javascript Ajax XHR加载失败,内部服务器错误为500,javascript,php,jquery,ajax,synology,Javascript,Php,Jquery,Ajax,Synology,非常感谢您的帮助。已经检查过与此标题类似的其他帖子,但不够具体 我试图在运行DSM 6.2.3-25426 Update 2的Synology DS415play上实现php脚本的ajax调用。我遵循这里给出的指导:使用三个文件:index.html、form.js和process.php 但是,form.js中的ajax调用失败,出现以下控制台消息: VM322:1 POST http://192.168.7.20/~user/process.php 500 (Internal Serv
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
三个源文件位于此处(均存储在用户/www文件夹中):
index.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>
看,我是阿贾兴!
处理AJAX表单
名称
提交
form.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();
});
});
$(文档).ready(函数(){
//处理表格
$('form').submit(函数(事件){
//获取表单数据
var formData={
'name':$('input[name=name]')。val()
};
//处理表格
$.ajax({
type:'POST',//定义要使用的HTTP谓词的类型(表单的POST)
url:'process.php',//我们要发布的url
data:formData,//我们的数据对象
dataType:'json',//我们希望从服务器返回什么类型的数据
编码:正确
})
//使用done承诺回调
.done(函数(数据){
//这里我们将处理错误和验证消息
如果(!data.success){
//处理名称------------------的错误
if(data.errors.name){
$(“#名称组”).addClass('has-error');//添加错误类以显示红色输入
$(“#名称组”).append(“”+data.errors.name+“”);//在输入下添加实际的错误消息
}
}否则{
//很好!只需显示成功消息!
$('form').html(“”+data.message+“”);
}
})
//使用失败承诺回调
.fail(函数(数据){
//服务器响应失败-显示错误消息
$('form').html('无法访问服务器,请稍后再试');
});
//停止表单以正常方式提交并刷新页面
event.preventDefault();
});
});
process.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);
它可能是中的空格!空($errors)
很多时候,错误500是PHP代码中的语法错误,您应该检查服务器错误日志,您可以在那里找到原因@KooiInc那里的空间应该不重要(即使有点难看),如果您可以从网络日志中显示您的请求参数,也可以尝试从PHP文件中删除所有内容并返回一些简单的内容,这将确保问题在代码中的某个地方,然后我们可以逐行调试PHP。您的控制台是否显示错误?这可能是因为CORS的政策原因感谢所有的建议,但仍然没有成功。我现在运行的是一个最小的PHP文件,它不带任何参数,只返回一个文本字符串。我设法找到了apache日志(而不是Synology PHP日志),并发现“[path]不在配置的docroot中”。但在Web站点PHP设置(核心)中设置doc_root没有效果。有什么建议吗?可能是中的空格!空($errors)
很多时候,错误500是PHP代码中的语法错误,您应该检查服务器错误日志,您可以在那里找到原因@KooiInc那里的空间应该不重要(即使有点难看),如果您可以从网络日志中显示您的请求参数,也可以尝试从PHP文件中删除所有内容并返回一些简单的内容,这将确保问题在代码中的某个地方,然后我们可以逐行调试PHP。您的控制台是否显示错误?这可能是因为CORS的政策原因感谢所有的建议,但仍然没有成功。我现在运行的是一个最小的PHP文件,它不带任何参数,只返回一个文本字符串。我设法找到了apache日志(而不是Synology PHP日志),并发现“[path]不在配置的docroot中”。但在Web站点PHP设置(核心)中设置doc_root没有效果。有什么建议吗?