GET 请求给出 404 错误 express js 即使我在我的 express 服务器中有一个定义明确的路由
GET request gives 404 error express js even though I have a well defined route in my express server
我正在使用 Jquery 的 ajax 请求从服务器获取 JSON 对象,但是当请求被调用时,它给出了一个 404 错误,如下所示:
Failed to load resource: the server responded with a status of 404 (Not Found) profile_data?uid=HW3gAHeiuJbQLMFQMbHA6nNlFUT2&_=1619880251360:1
就我而言,所有操作均按照标准完成,即 CRUD = Create:POST、Read:GET、更新:PUT 和删除:DELETE。
请让我知道我的代码中有什么是错的吗?
这是获取请求:
$(document).ready(()=>{
$.ajax({
url: '/profile_data',
type: 'GET',
cache: false,
data: { "uid": uid},
success: function(data){
//Instantiate Variables of User Data
}
, error: function(jqXHR, textStatus, err){
alert('text status '+textStatus+', err '+err)
}
});
});
} else {
Window.locate.assign("/login");
}
});
这是服务器端代码:
app.get("/profile_data", function (req, res){
const current_user = req.body;
const uid = current_user.uid;
console.log(uid)
db.ref("user_file_ref").on("value", snapshot=>{
snapshot.forEach( dataSnapshot=>{
if(uid === dataSnapshot.val().user_id){
getFileFromNet(dataSnapshot.val().ipfs_ref).then( result =>{
const hash = JSON.parse(result);
let decrypted_string = decrypt(hash);
let return_data = JSON.parse(decrypted_string);
//console.log("Here!!")
res.send(return_data);
}).catch(err =>{
console.log(err);
});
}
})
})
});
你应该用URL传递参数。
像这样:
$.ajax({
url: '/profile_data/' + uid,
type: 'GET',
cache: false,
// data: { "uid": uid}
在服务器端。
app.get("/profile_data/:uid", function (req, res){
const current_user = req.params.uid;
}
我正在使用 Jquery 的 ajax 请求从服务器获取 JSON 对象,但是当请求被调用时,它给出了一个 404 错误,如下所示:
Failed to load resource: the server responded with a status of 404 (Not Found) profile_data?uid=HW3gAHeiuJbQLMFQMbHA6nNlFUT2&_=1619880251360:1
就我而言,所有操作均按照标准完成,即 CRUD = Create:POST、Read:GET、更新:PUT 和删除:DELETE。 请让我知道我的代码中有什么是错的吗? 这是获取请求:
$(document).ready(()=>{
$.ajax({
url: '/profile_data',
type: 'GET',
cache: false,
data: { "uid": uid},
success: function(data){
//Instantiate Variables of User Data
}
, error: function(jqXHR, textStatus, err){
alert('text status '+textStatus+', err '+err)
}
});
});
} else {
Window.locate.assign("/login");
}
});
这是服务器端代码:
app.get("/profile_data", function (req, res){
const current_user = req.body;
const uid = current_user.uid;
console.log(uid)
db.ref("user_file_ref").on("value", snapshot=>{
snapshot.forEach( dataSnapshot=>{
if(uid === dataSnapshot.val().user_id){
getFileFromNet(dataSnapshot.val().ipfs_ref).then( result =>{
const hash = JSON.parse(result);
let decrypted_string = decrypt(hash);
let return_data = JSON.parse(decrypted_string);
//console.log("Here!!")
res.send(return_data);
}).catch(err =>{
console.log(err);
});
}
})
})
});
你应该用URL传递参数。
像这样:
$.ajax({
url: '/profile_data/' + uid,
type: 'GET',
cache: false,
// data: { "uid": uid}
在服务器端。
app.get("/profile_data/:uid", function (req, res){
const current_user = req.params.uid;
}