Jquery 使用fetch获取简单字符串
因此,我在API中有以下内容:Jquery 使用fetch获取简单字符串,jquery,node.js,rest,api,reactjs,Jquery,Node.js,Rest,Api,Reactjs,因此,我在API中有以下内容: router.get('/assetName', function(req, res, next){ //Get the token from the cookies var token = req.cookies.access_token; //Check it with the config secret if(jwt.verify(token, config.secret)){ //find from name in the requ
router.get('/assetName', function(req, res, next){
//Get the token from the cookies
var token = req.cookies.access_token;
//Check it with the config secret
if(jwt.verify(token, config.secret)){
//find from name in the request body
User.find({_id: req.query.id}).then(function(users){
var z;
var name = [];
var assetHolder = users[0].assets;
for(z=0; z < assetHolder.length; z++){
if(assetHolder[z]._id == req.query.vid){
name = assetHolder[z].name;
}
}
console.log(name);
res.send(name);
//Error handling
}).catch((err) => console.error(err));
}else{
res.send("Unauthorized");
}
});
我正在使用下面的获取请求:
var vehiclesT = asset.assetIDs;
if(!asset.assetIDs){
vehiclesT = [];
}
var y;
var assets = [];
for(y=0; y< assetsTemp.length; y++){
var url = 'api/assetName/?vid='+assetsTemp[y]+'&id='+id;
fetch(url, {credentials: 'include', method: 'get'}).then(function(data){
console.log(data);
vehicles.push(data);
});
}
所以车辆数组是空的
这是为什么?我如何通过fetch(或其他更好的方法)将值打印到API中的控制台
谢谢你,Ed.你的
取回过程中少了一步
:
fetch(url, {
credentials: 'include',
method: 'get'
})
.then(function(body){
return body.text(); // <--- THIS PART WAS MISSING
}).then(function(data) {
console.log(data);
vehicles.push(data);
});
fetch(url{
凭据:“包括”,
方法:“获取”
})
.然后(功能(主体){
返回body.text()//
Response {type: "basic", url: "http://localhost:4000/api/assetName/?vid=f4ufh49h49fh9fh94fh94hf&id=f484fh48fhfh98fh489fh", redirected: false, status: 200, ok: true…}
fetch(url, {
credentials: 'include',
method: 'get'
})
.then(function(body){
return body.text(); // <--- THIS PART WAS MISSING
}).then(function(data) {
console.log(data);
vehicles.push(data);
});