Javascript 有没有办法从同一个列表中检索项目并将它们注入到不同的页面?
我通过ajax调用从列表中提取项目。我构建html并将这些项目注入页面;我想抓取相同的项目,并将它们注入到不同的页面,样式不同。有没有办法链接两个ajax调用来获取相同的项目Javascript 有没有办法从同一个列表中检索项目并将它们注入到不同的页面?,javascript,jquery,ajax,sharepoint-2016,Javascript,Jquery,Ajax,Sharepoint 2016,我通过ajax调用从列表中提取项目。我构建html并将这些项目注入页面;我想抓取相同的项目,并将它们注入到不同的页面,样式不同。有没有办法链接两个ajax调用来获取相同的项目 $.ajax({ url: "/cyberSecurity/_api/web/lists/GetByTitle('phishingExamples')/items", method: 'GET', headers: { 'Accept': 'application/json; oda
$.ajax({
url: "/cyberSecurity/_api/web/lists/GetByTitle('phishingExamples')/items",
method: 'GET',
headers: {
'Accept': 'application/json; odata=verbose'
},
success: function(data) {
var items = data.d.results;
console.log(items);
var phishing = $('#phishing');
var phishingCards;
//Max of cards on page
var start = Math.max(0, items.length-4);
console.log(start);
for (let i = start; i < items.length; i++) {
phishingCards = '<div class="col-sm-6 col-md-6 col-lg-3 mb-3">' +
'<div style="background-color: #004685; height: 340px; position: relative;" class="card backImg2 ">' +
'<div style="color: #fff;" class="card-body ">' +
'<h5 style="color: #ffe01a;" class="card-title ">' + items[i].Title + '</h5>' +
'<p style="margin-bottom: -5px;" class="card-text ">' + items[i].Description + '</p>' +
'<div style="width: 100%; margin: 0 auto; position: absolute; bottom: 0;right: 0" class="row "><a style=" background-color: #ffe01a!important; color: black!important; border: none; width: 100%;" href= "'+ items[i].Image.Url +'"class="btn btn-primary btn-sm" target=_blank>More Info</a></div>' +
'</div>' +
'</div>' +
'</div>';
phishing.prepend(phishingCards);
}
},
error: function(data) {
console.log('Error: ' + data);
}
});
// End Service Icons //End Service Icons
$.ajax({
url:“/cyberSecurity/_api/web/List/GetByTitle('phishingExamples')/items”,
方法:“GET”,
标题:{
“接受”:“应用程序/json;odata=verbose”
},
成功:功能(数据){
var项目=数据和结果;
控制台日志(项目);
var网络钓鱼=$(“#网络钓鱼”);
var phishingCards;
//第页上的最大卡片数
var start=Math.max(0,items.length-4);
console.log(启动);
for(让i=start;i'+项目[i]。说明+”'+
'' +
'' +
'' +
'';
网络钓鱼。前置(网络钓鱼卡);
}
},
错误:函数(数据){
console.log('错误:'+数据);
}
});
//终端服务图标//终端服务图标
这是我到目前为止的代码您可以将代码分成不同的部分。并专注于不同的任务
var build_phishingCards = function(items){
var phishing = $('#phishing');
var phishingCards = ;
...
phishing.prepend(phishingCards);
}
var url1 = "/cyberSecurity/_api/web/lists/GetByTitle('phishingExamples')/items";
var handle_ajax = fucntion(url){
$.ajax({
url: url,
method: 'GET',
headers: {
'Accept': 'application/json; odata=verbose'
},
success: function(data) {
var items = data.d.results;
// only check out the data here. like, Array.isArray(items)
// call different function to do
if(data.layout == 'another'){
build_phishingCards(items);
}else{
build_AnOtherCards(items); // same list param
}
},
error: function(data) {
console.log('Error: ' + data);
}
});
}
handle_ajax(url1);
编辑:
var build_AnOtherCards = function(items){
var phishing = $('#phishing');
// handle other page layout
var AnOtherCards = '<div ...>';
...
phishing.prepend(AnOtherCards);
}
var build\u AnOtherCards=功能(项目){
var网络钓鱼=$(“#网络钓鱼”);
//处理其他页面布局
var AnOtherCards='';
...
网络钓鱼。预售(其他卡);
}
谢谢@henry。因此,如果我想在不同的页面布局上使用相同的列表,我唯一需要做的就是创建另一个函数并在成功函数中调用它,对吗?是的,并创建不同的函数,如build\u phishingCards
,它处理不同的页面布局。