如何 post url
How to post a url
我正在尝试将图像转换为 url,然后使用 axios.post 将其 post 转换为数据库,如下所示:
this.setState({
file: URL.createObjectURL(event.target.files[0])
})
然后 post 像这样:
axios
.post(`http://localhost:4000/items/${this.state.file}
我的模型是这样的:
let items = new schema ({
img: String
})
和我的 post 控制器:
router.post('/:urlImg', function (req, res) {
let b= new items ({
imageProof: req.params.urlImg
})
错误基本上是 POST 'url' 404 (Not Found):
xhr.js:184 POST http://localhost:4000/items/blob:http://localhost:3000/9045e921-4e7f-4541-8329-0b9cd65814c6 404 (Not Found)
但是需要注意的是,如果我使用的是简单的 url,例如 www.google.com,它会起作用。但是,我不能使用 https://
有谁知道我该如何解决这个问题?有没有其他方法来存储和显示图像?
您可以简单地 url 编码图像 url 如下。当您只是传递 url 而不先对其进行编码时,它会形成一个无效的 url,其中包含 https。所以你必须在传递之前对其进行编码
axios
.post(`http://localhost:4000/items/${encodeURIComponent(this.state.file)}
或
与其将其作为 url 参数发送,不如将其作为正文参数发送
axios.post('/user', {
urlImg: "http://your/image/url"
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
对于第二种方法,您可能需要更改后端以便从请求对象中提取参数。
我正在尝试将图像转换为 url,然后使用 axios.post 将其 post 转换为数据库,如下所示:
this.setState({
file: URL.createObjectURL(event.target.files[0])
})
然后 post 像这样:
axios
.post(`http://localhost:4000/items/${this.state.file}
我的模型是这样的:
let items = new schema ({
img: String
})
和我的 post 控制器:
router.post('/:urlImg', function (req, res) {
let b= new items ({
imageProof: req.params.urlImg
})
错误基本上是 POST 'url' 404 (Not Found):
xhr.js:184 POST http://localhost:4000/items/blob:http://localhost:3000/9045e921-4e7f-4541-8329-0b9cd65814c6 404 (Not Found)
但是需要注意的是,如果我使用的是简单的 url,例如 www.google.com,它会起作用。但是,我不能使用 https:// 有谁知道我该如何解决这个问题?有没有其他方法来存储和显示图像?
您可以简单地 url 编码图像 url 如下。当您只是传递 url 而不先对其进行编码时,它会形成一个无效的 url,其中包含 https。所以你必须在传递之前对其进行编码
axios
.post(`http://localhost:4000/items/${encodeURIComponent(this.state.file)}
或
与其将其作为 url 参数发送,不如将其作为正文参数发送
axios.post('/user', {
urlImg: "http://your/image/url"
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
对于第二种方法,您可能需要更改后端以便从请求对象中提取参数。