<!doctype html>
<html>
<head>
<title>axios - file upload example</title>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
</head>
<body class="container">
<h1>file upload</h1>
<form role="form" class="form" onsubmit="return false;">
<div class="form-group">
<label for="file">File</label>
<input id="file" type="file" class="form-control"/>
</div>
<button id="upload" type="button" class="btn btn-primary">Upload</button>
</form>
<div id="output" class="container"></div>
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.js"></script>
<script>
(function () {
var output = document.getElementById('output');
document.getElementById('upload').onclick = function () {
var data = new FormData();
data.append('foo', 'bar');
data.append('file', document.getElementById('file').files[0]);
var config = {
onUploadProgress: function(progressEvent) {
var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
}
};
axios.post('http://192.168.1.82/vill/base/file/upfile', data, config)
.then(function (res) {
output.className = 'container';
output.innerHTML = res.data;
})
.catch(function (err) {
output.className = 'container text-danger';
output.innerHTML = err.message;
});
};
})();
</script>
</body>
</html>
我使用本机js代码可以上传图片,我不明白为什么axios无法?..
我环顾四周,但没有找到任何原因。所以我需要你的帮助。
我环顾四周,但没有找到任何原因。所以我需要你的帮助。
我环顾四周,但没有找到任何原因。所以我需要你的帮助。
2VM788:1选项http://192.168.1.82/vill/base/file/upfile 403(禁止)
@ VM788:1
dispatchXhrRequest @ xhr.js:179
xhrAdapter @ xhr.js:13
dispatchRequest @ dispatchRequest.js:60
Promise.then (async)
request @ Axios.js:52
Axios.(anonymous function) @ Axios.js:72
wrap @ bind.js:10
document.getElementById.onclick @ (index):35
(index):1 Failed to load http://192.168.1.82/vill/base/file/upfile: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.43:8081' is therefore not allowed access. The response had HTTP status code 403.