'there is no request body consumed when it called by ajax
I got a problem with ajax when I call rest API post method, I use the nodeJS to build the API side the script looks like
const route = express.Router();
route.post('/tambahproduk',async(req,res)=>{
console.log("API 3");
try {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-
With, Content-Type, Accept");
let response = await ProdukController.tambahproduk(req,res)
return res.send(response)
} catch (error) {
console.log(error,`eror`);
return res.send(response("500",error))
}
})
the function that called from route bellow
async function tambahproduk(req,res){
const {
param1,param2
} = req.body;
let obj = {}
console.log(req.body,param1);
try {
const request = await DB.CONDB.promise();
let insert = `insert query`
console.log(insert);
// let data = await request.query(insert)
if(data){
obj = respone("200",`Success)
}else{
obj = respone("500",`Failed)
}
return obj
}catch(err){
console.log(err);
obj = respone("500",err)
return obj
}
}
so I try to call it with ajax in the client side
$.ajax({
type:'POST',
url:`${uri}/blabla/blablabla`,
data:{
param1: "asd",
param2: "123"
},
success: function(data){
console.log(data);
}
})
}
enter code here
but the response filed param1 is undefined, and even I tried to logging the req.body is there is no data consumed. please help me to fix it ? thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
