我正在使用此axios查询在我的node.js快递服务器上发送图片:
axios.post(this.server + 'images',
formData, {
crossdomain: true,
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then(function (result) {
console.log(result);
})
.catch(function (error) {
console.log(error);
});
图片现在在服务器上,但是,请看一下我在firefox控制台内部看到的这种奇怪行为,它说“网络错误”,而Axios接收到200状态!:
POST http://localhost/images
[HTTP/1.1 200 OK 20ms]
Error: "Network Error"
createError createError.js:16
handleError xhr.js:81
这是我的node.js后端Cors参数:
const cors = require("cors");
app.use(
require("cors")({
origin: function(origin, callback) {
callback(null, origin);
}
})
);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", req.headers.origin); // update to match the domain you will make the request from
//to allow cross domain requests to send cookie information.
res.header("Access-Control-Allow-Credentials", true);
// list of methods that are supported by the server
res.header("Access-Control-Allow-Methods", "OPTIONS,GET,PUT,POST,DELETE");
res.header(
"Access-Control-Allow-Headers",
"X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, X-XSRF-TOKEN"
);
next();
});
这是一个大问题,因为我需要能够利用the.then()响应,而我不能。
我还应该做什么?
注意: 这是我获取图片的节点功能,请注意,图片上传后,我将发送200状态:
app.post("/images", upload.single("file"), function(req, res, next) {
sharp(req.file.path)
.resize(200, 200)
.toBuffer(function(err, buffer) {
fs.writeFile(req.file.path, buffer, function(e) {});
});
res.sendStatus(200);
});
编辑将我的节点服务器端口更改为3000: 问题依旧:
POST http://localhost:3000/images
[HTTP/1.1 200 OK 11ms]
Error: "Network Error"
createError createError.js:16
handleError xhr.js:81
如何获得结果答案?
我需要获取重命名的图片,并且无法访问.then()axios函数。
我无法在axios .then()中获取nico.jpg-5454545,它始终显示“网络错误”
编辑3: 像这样没有运气就改变了我的后端参数:(这是webpack应用程序端口)
res.header("Access-Control-Allow-Origin", "http://localhost:8081");
编辑4: 这样就改变了我的axios查询,但是没有运气(这是node.js服务器端口):
axios.post(this.server + 'images',
formData, {
crossdomain: true,
headers: {
'Content-Type': 'multipart/form-data',
'Access-Control-Allow-Origin': "http://localhost:3000"
}
}
).then(function (e) {
if (e){
console.log(e);
}
})
.catch(function (err ) {
if (err.message === "Network Error"){
console.log(err); // Works but nothing gets shown
}
});
},
编辑5:
尝试了此代码后,我没有运气,我需要访问响应数据,并且只会收到“ NETWORK ERROR”(网络错误)
const send = async () => {
try {
return await axios.post(this.server + 'images',
formData, {
crossdomain: true,
headers: {
'Content-Type': 'multipart/form-data'
}
}
)
} catch (error) {
console.error(error)
}
}
const myFilename = send()
console.log(myFilename);
我正在尝试切换到vue-resource,我真的有足够的力量!
编辑6::似乎仍然是CORS问题:
编辑7:已解决!!!
问题是我的整个CORS代码位于node.js服务器上的app.js内部的app.post(“ / images” ...函数下。 cors代码需要保留在app.js node.js服务器文件的顶部。
现在这是我的CORS代码,请注意,现在是在图像Web服务之前:
// ----------------------------------- CORS -------------------------------------------
const cors = require("cors");
app.use(require("cors")());
app.use(
cors({
origin: ["http://localhost:8081", "http://localhost:8081/images"],
credentials: true
})
);
// ----------------------------------- END OF CORS -------------------------------------------
app.post("/images", upload.single("file"), function(req, res, next) {
console.log(req.file);
sharp(req.file.path)
.resize(200, 200)
.toBuffer(function(err, buffer) {
fs.writeFile(req.file.path, buffer, function(e) {});
});
res.send({ filename: req.file.filename });
// res.sendStatus(200);
});
答案 0 :(得分:0)
编辑7:已解决!!!
问题是我的整个CORS代码是写在node.js服务器上app.js内部的app.post(“ / images” ...函数下的。 ,Cors代码需要保留在app.js node.js服务器文件的顶部。
现在这是我的CORS代码,请注意,现在是在图像Web服务之前:
// ----------------------------------- CORS -------------------------------------------
const cors = require("cors");
app.use(require("cors")());
app.use(
cors({
origin: ["http://localhost:8081", "http://localhost:8081/images"],
credentials: true
})
);
// ----------------------------------- END OF CORS -------------------------------------------
app.post("/images", upload.single("file"), function(req, res, next) {
console.log(req.file);
sharp(req.file.path)
.resize(200, 200)
.toBuffer(function(err, buffer) {
fs.writeFile(req.file.path, buffer, function(e) {});
});
res.send({ filename: req.file.filename });
// res.sendStatus(200);
});