let formData2 = new FormData();
formData2.append('_token', vm.response._token);
formData2.append('file', vm.response.content[i].path);
formData2.append('type', vm.response.content[i].type);
// this part is progress bar
var xhr = new XMLHttpRequest();
xhr.open("POST", "page/file/create/upload", true);
if (xhr.upload) {
xhr.upload.onprogress = function (e) {
if (e.lengthComputable) {
progressBar.max = e.total;
progressBar.value = e.loaded;
display.innerText = Math.floor((e.loaded / e.total) * 100) + '%';
}
}
xhr.upload.onloadstart = function (e) {
progressBar.value = 0;
display.innerText = '0%';
}
xhr.upload.onloadend = function (e) {
progressBar.value = e.loaded;
}
}
xhr.send(formData2);
我在按下f12后在网络选项卡中看到,在响应标题中我的内容类型是text / html。我认为这是获得500错误的主要原因。因为在ajax中需要JSON。我正在使用laravel,我的控制器得到NULL。 那怎么能把它变成json?
答案 0 :(得分:0)
let formData2 = new FormData();
formData2.append('file', vm.response.content[i].path);
// this part is progress bar
var xhr = new XMLHttpRequest();
xhr.open("POST", "page/file/create/upload", true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.setRequestHeader('X-CSRF-TOKEN', vm.response._token);
if (xhr.upload) {
xhr.upload.onprogress = function (e) {
if (e.lengthComputable) {
progressBar.max = e.total;
progressBar.value = e.loaded;
display.innerText = Math.floor((e.loaded / e.total) * 100) + '%';
}
}
xhr.upload.onloadstart = function (e) {
progressBar.value = 0;
display.innerText = '0%';
}
xhr.upload.onloadend = function (e) {
progressBar.value = e.loaded;
}
}
xhr.send(formData2);
@ksoni仍然有500个错误代码。而content-header仍然是text / html。我用formData2(图像或任何文件)做了什么错误。什么是程序。你可以解释一下吗。