[英]jQuery AJAX to upload picture => PHP sends back responseText with HTML instead of JSON
我的目标是通过jQuery AJAX将图像上传到PHP SYMFONY 。 多亏了我在其他SO线程上的输入,我设法将文件发送到PHP端。 现在, 我期望从PHP返回AJAX的答案是JSON 。
我可以看到我的PHP创建了JSON并将其发送回去,但是由于某些原因(我怀疑是PHP.INI设置),AJAX对象在其responseText
中的开头有一些HTML:
"<br /> <b>Notice</b>: Unknown: file created in the system's temporary directory in <b>Unknown</b> on line <b>0</b><br /> "{\u0022status\u0022:\u0022success\u0022,\u0022fileUploaded\u0022:true}""
我在AJAX方面的代码如下所示:
var pictureFormData = new FormData();
pictureFormData.append('pictureFile', $(#[input_type_file_on_DOM_id])[0].files[0]);
$.ajax({
url: [my php to integrate image].php,
data: pictureFormData,
type:"post",
contentType:false,
processData:false,
cache:false,
dataType:"json",
success:function(res){
console.log('AJAX SUCCESS');
console.info(res);
},
error:function(err){
console.log('AJAX ERR');
console.info(err);
},
timeout:function(){
console.log('AJAX TIMEOUT');
},
complete: function(){
console.log('AJAX COMPLETE');
}
});
在我的PHP SYMFONY一侧,处理AJAX调用的控制器如下所示:
class AjaxImageController extends Controller
{
public function indexAction(Request $request)
{
$file = $request->files->get('pictureFile');
$status = array('status' => "success","fileUploaded" => false);
// If a file was uploaded
if(!is_null($file)){
// generate a random name for the file but keep the extension
$filename = uniqid().".".$file->getClientOriginalExtension();
$path = "/tmp";
$file->move($path,$filename); // move the file to a path
$status = array('status' => "success","fileUploaded" => true);
}
$data = json_encode($status);
$response = new JsonResponse();
$response->setData($data);
dump($response);
return $response;
}
}
如您所见,我只是在将其发送回之前进行了dump(response)
。 其内容如下:
JsonResponse {#719 ▼
#data: ""{\u0022status\u0022:\u0022success\u0022,\u0022fileUploaded\u0022:true}""
#callback: null
#encodingOptions: 15
+headers: ResponseHeaderBag {#722 ▼
#computedCacheControl: array:2 [▼
"no-cache" => true
"private" => true
]
#cookies: []
#headerNames: array:2 [▼
"cache-control" => "Cache-Control"
"content-type" => "Content-Type"
]
#headers: array:2 [▼
"cache-control" => array:1 [▼
0 => "no-cache, private"
]
"content-type" => array:1 [▼
0 => "application/json"
]
]
#cacheControl: []
}
#content: ""{\u0022status\u0022:\u0022success\u0022,\u0022fileUploaded\u0022:true}""
#version: "1.0"
#statusCode: 200
#statusText: "OK"
#charset: null
}
因此,我不明白为什么responseText
错误地以HTML开头: "<br /> <b>Notice</b>: Unknown: file created in the system's temporary directory in <b>Unknown</b> on line <b>0</b><br />"
,然后触发AJAX调用的error
功能。
一些更新 :我注意到以下内容:
在客户端,当我有pictureFormData.append('pictureFile', $(#[input_type_file_on_DOM_id])[0].files[0]);
嵌入到我发送到SYMFONY PHP Server的文件中:我注意到在CONTROLLER传入的$request
,该文件出现在FileBag对象中:
Request {#9 ▼
+attributes: ParameterBag {#12 ▶}
+request: ParameterBag {#10 ▶}
+query: ParameterBag {#11 ▶}
+server: ServerBag {#16 ▶}
+files: FileBag {#14 ▼
#parameters: array:1 [▼
"pictureFile" => UploadedFile {#15 ▼
.... all files data
}
]
}
...
}
然后我得到我描述的错误 。
如果我拿走了从客户端发送的文件,并检查了CONTROLLER,则$request
看起来像这样:
Request {#9 ▼
... +files: FileBag {#14 ▼
#parameters: []
}
...
}
在这种情况下,错误不会出现。 因此,我怀疑对FileBag对象执行的操作会以某种方式引发echo
。
您的响应包含HTML,因为php抛出错误。 如果您更正它,那么响应将很好。
似乎该文件不存在。
如果在服务器上或XHR请求中看不到文件,则需要在enctype="multipart/form-data"
上添加enctype="multipart/form-data"
。
如果表单和XHR请求都可以,请尝试将根目录添加到您的上传目录中来上传文件。
# app/config/config.yml
# ...
parameters:
brochures_directory: '%kernel.root_dir%/../web/uploads/brochures'
和
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);
或只是在您的控制器中
$root = $this->get('kernel')->getRootDir();
$path = $root."/tmp";
$file->move($path,$filename); // move the file to a path
该问题归因于PHP.INI设置:
文件夹: upload_tmp_dir = [php的路径] \\ php7 \\ uploadtemp在PHP.INI中设置。
存在“ [php的路径] \\ php7 \\”,但没有文件夹“ uploadtemp”,它无法创建它。
我在“ [php的路径] \\ php7 \\”中创建了文件夹“ uploadtemp”,它就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.