我想知道如何在Ajax中发送数组数据,对于每个数组在javascript中返回相应的ID
我有静态数据,我需要为每个静态对象在前端加载后进行ajax调用,如何将id
发送到服务器以获取已加载对象并返回相应的apidata >
例如,加载obj insta
,然后通过向服务器发送ID并返回instaapi
//backend
var express = require('express');
var app = express();
var router = express.Router();
var instaapi=[{
id: "insta",
country: "SG",
rate: 0.5
}]
var remitapi=[{
id: "remit",
country: "TH",
rate: 1.5
}]
router.get('/en', function (req, res) {
if(req.query.hasOwnProperty("id")){
res.send({instaapi: instaapi, remitapi:remitapi})
}
else{
res.send("error")
}
}
//frontend
var pr=["insta","remit"]
static(){
var li = $.ajax({
url: "/en",
method: 'get',
global: false,
async: false,
data: {
id: JSON.stringify(pr) //passing array data
},
success: function (data) {
return data;
}
}).responseText;
var result=JSON.parse(li);
console.log(result);
}