我的代码不能正常工作,3个小时后,我不明白为什么它不能工作。有人能帮我吗?我的目标是用jQuery将表单字段和图像发送到PHP。我没有得到任何回应作为XHR。我唯一看到的是(参数):
-----------------------------163541139317822
Content-Disposition: form-data; name="staffnumber"
utyu
-----------------------------163541139317822
Content-Disposition: form-data; name="nameperson"
yu
-----------------------------163541139317822
Content-Disposition: form-data; name="fileupload"; filename=""
Content-Type: application/octet-stream
-----------------------------163541139317822--控制台中的错误: SyntaxError: JSON.parse: JSON数据第1列中数据的意外结束。
<form method="POST" id="formadduser" enctype="multipart/form-data">
<div id="staffnumber" class="form-group row">
<label for="staffnumber" class="col-sm-3 col-form-label"><strong>Personeelsnummer</strong> <img src="api/icons/info.svg"></label>
<div class="col-sm-4">
<input class="form-control" id="staffnumber" name="staffnumber" value="" type="text">
</div>
</div>
<div id="nameperson" class="form-group row">
<label for="nameperson" class="col-sm-3 col-form-label"><strong>Naam</strong> <img src="api/icons/info.svg"></label>
<div class="col-sm-4">
<input class="form-control" id="nameperson" name="nameperson" value="" type="text">
</div>
</div>
<div id="rowstarttime" class="form-group row">
<label for="starttime" class="col-sm-3 col-form-label"><strong>Afbeelding</strong></label>
<div class="col-sm-4">
<label class="custom-file">
<input type="file" id="fileupload" class="custom-file-input" name="fileupload">
<span class="custom-file-control"></span>
</label>
</div>
</div>
<div class="modal-footer">
<button id="modaladdusersave" class="btn btn-primary mx-sm-3" type="submit">Opslaan</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Sluiten</button>
</form>jQuery
$("#formadduser").submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
alert(formData);
alert('hoi');
$.ajax({
ajax_call: "add_user",
url: '/api/add.php',
type: 'POST',
data: formData,
async: false,
success: function (data) {
var data = $.parseJSON( data );
alert(data)
},
cache: false,
contentType: false,
processData: false
});
return false;
});<?php
require_once('../database.php');
if(isset($_GET['ajax_call']) && $_GET['ajax_call'] == 'add_user') {
$staffnumber = $_GET['staffnumber']; // $_GET['staffnumber'];
$nameperson = $_GET['nameperson']; // $_GET['nameperson'];
$file = $_FILES['fileupload']['tmp_name'];
$targetPath = "upload/".$_FILES['fileupload']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file
// Add record
// $queryNew = 'INSERT INTO Persons (id, name, image) VALUES ("'. $staffnumber .'", "'. $nameperson .'", "test")';
// mysqli_query($conn, $queryNew);
$data = array(
'staffnumber' => $staffnumber,
'nameperson' => $nameperson,
);
echo json_encode($data);
exit;
}
?>发布于 2017-11-13 10:05:16
使用post方法而不是get在php文件中获取记录,因为您是通过jquery在post方法中传递数据,
if(isset($_GET['ajax_call']) && $_GET['ajax_call'] == 'add_user') {
$staffnumber = $_GET['staffnumber']; // $_GET['staffnumber'];
$nameperson = $_GET['nameperson'];而不是
if(isset($_POST['ajax_call']) && $_POST['ajax_call'] == 'add_user') {
$staffnumber = $_POST['staffnumber'];
$nameperson = $_POST['nameperson'];这里的move_uploaded_file($sourcePath,$targetPath)源路径是什么??,您必须在这里传递临时文件move_uploaded_file($_FILES['fileupload']['tmp_name'],$targetPath) ;。
发布于 2017-11-13 11:09:43
我认为您应该处理文件的返回值。
作为PHP
如果文件名不是有效的上载文件,则不会发生任何操作,move_uploaded_file()将返回FALSE。
如果文件名是有效的上载文件,但由于某种原因无法移动,则不会发生任何操作,move_uploaded_file()将返回FALSE。此外,还将发出警告。
$moved = move_uploaded_file($_FILES['fileupload']['tmp_name'],$targetPath)
if( $moved ) {
$data = array(
'staffnumber' => $staffnumber,
'nameperson' => $nameperson,
);
echo json_encode($data);
} else {//something went wrong
$data = array(
'error' => $_FILES["file"]["error"]
);
echo json_encode($data);
}它将为您提供以下错误代码值1至8之一:
UPLOAD_ERR_INI_SIZE = Value: 1;上传的文件超出了php.ini中的upload_max_filesize指令。
UPLOAD_ERR_FORM_SIZE = Value: 2;上传的文件超过了HTML中指定的MAX_FILE_SIZE指令。
UPLOAD_ERR_PARTIAL = Value: 3;上传的文件仅被部分上传。
UPLOAD_ERR_NO_FILE =值: 4;没有上传任何文件。
UPLOAD_ERR_NO_TMP_DIR =值: 6;缺少一个临时文件夹。在PHP 5.0.3中引入。
UPLOAD_ERR_CANT_WRITE = Value: 7;未能将文件写入磁盘。在PHP5.1.0中引入。
UPLOAD_ERR_EXTENSION = Value: 8;一个PHP扩展名停止了文件上传。PHP没有提供一种方法来确定哪个扩展名导致文件上传停止;使用phpinfo()检查加载的扩展名列表可能会有所帮助。
https://stackoverflow.com/questions/47261593
复制相似问题