错误 405:资源不支持指定的 http 谓词



我正在研究发布在 https://scotch.io/tutorials/how-to-handle-file-uploads-in-vue-2 上的 vue.js教程,我正在尝试修改上传网站以使用Microsoft Azure 存储 blob。但是,在我修改了一个名为"file-upload.service.js"的文件后,我收到一个 405 错误:资源不支持指定的 http 动词 我已经检查了 CORS 设置: 允许的原产地 * 允许的方法 检查所有 http 动词 允许的标头 * 公开的标头内容长度 最大年龄 84600

  1. 我已将常量BASE_URL = 'http://localhost:3001'; 行更改为 https://XXXXXXXXX.z4.web.core.windows.net/
  2. 更改了常量网址 =${BASE_URL}/photos/upload; 更改为常量网址 =${BASE_URL}/images;
  3. 更改了 img 行,{ 网址:${BASE_URL}/images/${img.id}})));到 img, { url:${BASE_URL}/images/${img.id}/${sasToken}})));

这是原始代码: 文件上传服务.js

import * as axios from 'axios';
const BASE_URL = 'http://localhost:3001';
function upload(formData) {
const url = `${BASE_URL}/photos/upload`;
return axios.post(url, formData)
// get data
.then(x => x.data)
// add url field
.then(x => x.map(img => Object.assign({},
img, { url: `${BASE_URL}/images/${img.id}` })));
}
export { upload }

这是更改后代码的样子:

const BASE_URL = 'https://XXXXXXXX.blob.core.windows.net';
const AccountKey = '?XXXXXXXXXXXXXXXXX'
function upload(formData) {
const uri = `${BASE_URL}/images `;
return axios.post(uri, formData)
// get data
.then(x => x.data)
// add url field
.then(x => x.map(img => Object.assign({},
img, { uri: `${BASE_URL}/images/${img.id}/${AccountKey}` })));
}
export { upload }

预期结果:图像将上传到存储 Blob 实际结果:错误 405:资源不支持指定的 HTTP 谓词

{
"data": "<?xml version="1.0" encoding="utf-8"?><Error><Code>UnsupportedHttpVerb</Code><Message>The resource doesn't support specified Http Verb.nRequestId:4d9b12be-e01e-0060-759b-1dfd43000000nTime:2019-06-08T01:46:25.1276517Z</Message></Error>",
"status": 405,
"statusText": "The resource doesn't support specified Http Verb.",
"headers": {
"content-length": "237",
"content-type": "application/xml"
},
"config": {
"url": "https://XXXXXXXXX.blob.core.windows.net/images ",
"method": "post",
"data": {},
"headers": {
"Accept": "application/json, text/plain, */*"
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1
},
"request": {}
}

收到此错误的原因是上传 blob 的 HTTP 方法是PUT的,而不是POST:https://learn.microsoft.com/en-us/rest/api/storageservices/put-blob。

最新更新