I have ajax to fetch some json data. And then, these data will be displayed on web page.
I find some code which is duplicated in success:function(data).
Now, I want to refactor part of code in success:function(data) to a function homPag(). But it is fail.
Here is the code, it works ok:
<script>
$(function() {
$("#header").load("/header.html");
$.ajax({
url:'get_subnet_resourcebk.json',
dataType:'json',
type:'get',
data:{},
success:function(data)
{
var dataL = data.list20.length+data.list19.length+data.list18.length;
var pageNum = Math.ceil(dataL/10);
var htmlC = "";
htmlC += " <div class='newsLBox fL'>";
htmlC += "ALL:";
htmlC += dataL;
htmlC += "pages";
htmlC += " </div>";
for (var i = 0; i < 5 ; i++)
{
var tit = data.list20[i].title;
htmlC += "<div class='newsLBox fL'>";
htmlC += tit;
htmlC += "</div>";
}
$('#test').html(htmlC);
}
});
});
</script>
Here is the code, it works fail:
<script>
$(function() {
$("#header").load("/header.html");
alert("testxiexie");
function homPag()({
htmlC += " <div class='newsLBox fL'>";
htmlC += " ALL:";
htmlC += dataL;
htmlC += "pages";
htmlC += " </div>";
for (var i = 0; i < 5 ; i++)
{
var tit = data.list20[i].title;
htmlC += "<div class='newsLBox fL'>";
htmlC += tit;
htmlC += "</div>";
}
});
$.ajax({
url:'get_subnet_resourcebk.json',
dataType:'json',
type:'get',
data:{},
success:function(data)
{
var dataL = data.list20.length+data.list19.length+data.list18.length;
var pageNum = Math.ceil(dataL/10);
var htmlC = "";
homPag(); //it works wrong
$('#test').html(htmlC);
}
});
</script>
My json get_subnet_resourcebk.json data is like:
{
"list20": [
{
"title":" 1. Robust Twin",
"author":" Rui Wang* and Hu u*.",
"journal": "Pett. 2019. 057205"
},
{
"title":" 1. eyl Fermioides",
"author":"Bowen Xia, Yuanjn, Baobing Zh.",
"journal": "Phy9. 122 0505"
}
],
"list19": [
{
"title":" 2. ermions in ",
"author":"ia Chen, BaobXu*.",
"journal": "Ph05"
},
{
"title":" 2. Roberromagnetic Oxides",
"author":"Bo Zheng, YXu*.",
"journal": "Phy122 057205"
},
{
"title":" 2. eyl Fermioc O",
"author":"BowZhongjia Wang* and H*.",
"journal": "Phys. 9. 122 057205"
}
],
"list18": [
{
"title":" 3. in Ferroic Oxides",
"author":"Bao, Zhongjia Chen, Baoo, Rui Wau Xu*.",
"journal": "Phys. Re057205"
}
]
}
who can give me a help?