이것은 바보 같은 질문 일 수 있으며 NativeScript를 처음 사용했습니다. 나는 샘플 프로젝트를 가지고있다. 간단한 요청을 로컬 api로 보내려고합니다. 이것은 내 지역 dev에 컴퓨터에서 웹 사이트 설정입니다 : 나는 내 로컬 API에 엔드 포인트에 액세스하려고하고 아무것도 반환하지 않는 것로컬 API로 Nativecript 앱 개발
. 하지만 API 끝점을 로컬 API가 아닌 무언가로 변경할 수 있으며 가끔은 반환 할 수 있습니다.
API는 Laravel/PHP API이며 유효한 json을 반환한다는 사실을 확인할 수 있으므로 API가 문제라고 생각하지 않습니다. 여기 내 파일이있다.
var config = require("../../shared/config");
var fetchModule = require("fetch");
//var http = require("http");
var ObservableArray = require("data/observable-array").ObservableArray;
function GroceryListViewModel(items) {
var viewModel = new ObservableArray(items);
viewModel.load = function() {
// http.getJSON(config.apiUrl + "events").then(function(result) {
// console.log(JSON.stringify(result));
// }, function(error) {
// console.error(JSON.stringify(error));
// });
return fetch(config.apiUrl + "events", {
headers: {
//"Authorization": "Bearer " + config.token
"Content-Type": "application/json"
}
})
.then(handleErrors)
.then(function(response) {
console.log(response);
return response.json();
}).then(function(data) {
console.dump(data);
data.forEach(function(event) {
viewModel.push({
name: event.name,
id: event.id
});
});
});
};
viewModel.empty = function() {
while (viewModel.length) {
viewModel.pop();
}
};
return viewModel;
}
function handleErrors(response) {
if (!response.ok) {
console.log(JSON.stringify(response));
throw Error(response.statusText);
}
return response;
}
module.exports = GroceryListViewModel;
제발 나에게 쉽게이 도움을 요청하십시오. 모두에게 미리 감사드립니다.
예. 올바르게 설정되고 올바른 로컬 api 끝점을 가리 킵니다. 데이터는 유효한 JSON 객체도 반환합니다. 어떤 이유에서이 로그 문에는 아무 것도 표시되지 않습니다. 가이드의 API와 같이 외부 API로 변경하면 올바르게 작동합니다. – devcflynn
내 컴퓨터와 관련된 포트가 있는지 확실하지 않았습니다. 나는 LEMP env도 Unbuntu 16.04 머신에서 실행 중이다. – devcflynn
데이터를 반환하는 경우 모두 괜찮습니다. 이 출력을 볼 때 출력을 추가 할 수 있습니까? –