未根据本机脚本中 json api 的请求加载
Not loading on request from the json api in nativescript
主-page.xml
<Page loaded="pageLoaded" class="page" xmlns="http://www.nativescript.org/tns.xsd"
xmlns:lv="nativescript-ui-listview"
xmlns:Card="nativescript-cardview">
<ActionBar title="Home" class="action-bar">
</ActionBar>
<lv:RadListView id="ls" items="{{ dataItems }}" row="0" loadOnDemandMode="Auto"
loadMoreDataRequested="{{onLoadMoreItemsRequested}}">
<lv:RadListView.itemTemplate>
<Card:CardView class="card" elevation="40" radius="10" >
<StackLayout class="card-layout" orientation="horizontal" >
<StackLayout class="card-layout" orientation="vertical" >
<Label fontSize="20" text="{{ title }}" marginBottom="8" />
</StackLayout>
</StackLayout>
</Card:CardView>
</lv:RadListView.itemTemplate>
</lv:RadListView>
</Page>
主-page.js
var HomeViewModel = require("./main-view-model");
var homeViewModel = new HomeViewModel();
function pageLoaded(args) {
var page = args.object;
page.bindingContext = homeViewModel;
listView = page.getViewById("ls");
homeViewModel.initDataItems();
homeViewModel.addMoreItemsFromSource(3);
}
exports.pageLoaded = pageLoaded;
主视图-model.js
const httpModule = require("tns-core-modules/http");
var observableModule = require("tns-core-modules/data/observable");
var ObservableArray = require("tns-core-modules/data/observable-array").ObservableArray;
var posts = require("./posts.json");
var url1="https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Janelle%20Mon%C3%A1e&prop=categories";
function HomeViewModel() {
var viewModel = observableModule.fromObject({
_sourceDataItems: [],
initDataItems: function () {
*//This function is for local json Its working as expected.
this._sourceDataItems = new ObservableArray();
for (let i = 0; i < posts.names.length; i++) {
this._sourceDataItems.push({ name: posts.names[i] });
}//*
httpModule.request({
url: url1,
method: "GET"
}).then((response) => {
for( var i=0; i<response.length;i++){
this. _sourceDataItems.push({title: response.query.pages.categorymembers[i].title})
}
}, (e) => {
});
},
dataItems: [],
addMoreItemsFromSource: function (chunkSize) {
let newItems = this._sourceDataItems.splice(0, chunkSize);
this.dataItems = this.dataItems.concat(newItems);
},
onLoadMoreItemsRequested: function (args) {
console.log("---load item called----");
const that = new WeakRef(this);
const listView = args.object;
if (this._sourceDataItems.length > 0) {
setTimeout(function () {
that.get().addMoreItemsFromSource(3);
listView.notifyLoadOnDemandFinished();
}, 4000);
args.returnValue = true;
} else {
args.returnValue = false;
listView.notifyLoadOnDemandFinished(false);
}
},
});
return viewModel;
}
module.exports = HomeViewModel;
在我的代码片段中,我无法从 json api 获取数据,而是从本地 json 获取数据。如果我把 json api 数据不显示,也不给出错误。请帮助我在加载更多数据请求时从 api 获取数据。
https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Janelle%20Mon%C3%A1e&prop=categories,这是在编写使用 fetch() 的函数时用于演示,httpmodule.getjson() 它不适用于所有人让我知道我在哪里犯了错误。
正如我已经提到的,您没有正确解析数据。
这就是你的 JSON 的样子,
{
...
"query":{
"pages":{
"13828397":{
...
"categories":[
{
"ns":14,
"title":"Category:1985 births"
},
]
}
}
}
}
所以你必须这样做,
var items = response.query.pages["13828397"].categories;
for( var i=0; i<items.length;i++){
this._sourceDataItems.push({title: items[i].title})
}
主-page.xml
<Page loaded="pageLoaded" class="page" xmlns="http://www.nativescript.org/tns.xsd"
xmlns:lv="nativescript-ui-listview"
xmlns:Card="nativescript-cardview">
<ActionBar title="Home" class="action-bar">
</ActionBar>
<lv:RadListView id="ls" items="{{ dataItems }}" row="0" loadOnDemandMode="Auto"
loadMoreDataRequested="{{onLoadMoreItemsRequested}}">
<lv:RadListView.itemTemplate>
<Card:CardView class="card" elevation="40" radius="10" >
<StackLayout class="card-layout" orientation="horizontal" >
<StackLayout class="card-layout" orientation="vertical" >
<Label fontSize="20" text="{{ title }}" marginBottom="8" />
</StackLayout>
</StackLayout>
</Card:CardView>
</lv:RadListView.itemTemplate>
</lv:RadListView>
</Page>
主-page.js
var HomeViewModel = require("./main-view-model");
var homeViewModel = new HomeViewModel();
function pageLoaded(args) {
var page = args.object;
page.bindingContext = homeViewModel;
listView = page.getViewById("ls");
homeViewModel.initDataItems();
homeViewModel.addMoreItemsFromSource(3);
}
exports.pageLoaded = pageLoaded;
主视图-model.js
const httpModule = require("tns-core-modules/http");
var observableModule = require("tns-core-modules/data/observable");
var ObservableArray = require("tns-core-modules/data/observable-array").ObservableArray;
var posts = require("./posts.json");
var url1="https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Janelle%20Mon%C3%A1e&prop=categories";
function HomeViewModel() {
var viewModel = observableModule.fromObject({
_sourceDataItems: [],
initDataItems: function () {
*//This function is for local json Its working as expected.
this._sourceDataItems = new ObservableArray();
for (let i = 0; i < posts.names.length; i++) {
this._sourceDataItems.push({ name: posts.names[i] });
}//*
httpModule.request({
url: url1,
method: "GET"
}).then((response) => {
for( var i=0; i<response.length;i++){
this. _sourceDataItems.push({title: response.query.pages.categorymembers[i].title})
}
}, (e) => {
});
},
dataItems: [],
addMoreItemsFromSource: function (chunkSize) {
let newItems = this._sourceDataItems.splice(0, chunkSize);
this.dataItems = this.dataItems.concat(newItems);
},
onLoadMoreItemsRequested: function (args) {
console.log("---load item called----");
const that = new WeakRef(this);
const listView = args.object;
if (this._sourceDataItems.length > 0) {
setTimeout(function () {
that.get().addMoreItemsFromSource(3);
listView.notifyLoadOnDemandFinished();
}, 4000);
args.returnValue = true;
} else {
args.returnValue = false;
listView.notifyLoadOnDemandFinished(false);
}
},
});
return viewModel;
}
module.exports = HomeViewModel;
在我的代码片段中,我无法从 json api 获取数据,而是从本地 json 获取数据。如果我把 json api 数据不显示,也不给出错误。请帮助我在加载更多数据请求时从 api 获取数据。 https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Janelle%20Mon%C3%A1e&prop=categories,这是在编写使用 fetch() 的函数时用于演示,httpmodule.getjson() 它不适用于所有人让我知道我在哪里犯了错误。
正如我已经提到的,您没有正确解析数据。
这就是你的 JSON 的样子,
{
...
"query":{
"pages":{
"13828397":{
...
"categories":[
{
"ns":14,
"title":"Category:1985 births"
},
]
}
}
}
}
所以你必须这样做,
var items = response.query.pages["13828397"].categories;
for( var i=0; i<items.length;i++){
this._sourceDataItems.push({title: items[i].title})
}