JQuery Ajax 上传图片到图床时的跨域问题
写了一个上传图片js文件,其中通过$.ajax 上传文件到图床:https://sm.ms/api/v2/upload?inajax=1一直报错:Access to XMLHttpRequest at 'https://sm.ms/api/v2/upload?inajax=1' from origin 'http://humingnote2' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
'http://humingnote2'是本地iis配置服务器名
网上说用jsonp的方式,但是改图床时外部url是POST方式,无法通过jsonp方式来传递
以下时部分代码:
<head>
<!-- header 设置Access-Control-Allow-Origin貌似不起作用 -->
<meta http-equiv="Access-Control-Allow-Origin" content="*">
</head>
<body>
<form enctype="multipart/form-data">
<input name="file" type="file" id="file" />
</form>
<script>
$(':input').change(function () {
var f = this.files[0];
var formData = new FormData();
formData.append('smfile', f);
$.ajax({
url: "https://sm.ms/api/v2/upload?inajax=1",
type: "POST",
success: function (data) {
console.log(data);
},
data: formData,
cache: false,
contentType: false,
processData: false
});
}
</script>
</body>
[此贴子已经被作者于2021-9-2 09:42编辑过]