问题
This may be a silly question and I am very new to NativeScript. I have a sample project up. Trying to do a simple get request to a local api. This is a website setup on my local dev machine for:
http://test-api.dev/api/v1
I am trying to access an endpoint on my local API and it does not seem to return ANYTHING. But I can change the API endpoint to something that is not a local API and sometimes it return stuff.
The API is a Laravel/PHP api and I can confirm it is infact returning valid json, so I don't think the API is the problem. Here is my file.
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;
Please go easy on me, definitely seeking help on this though. Thank you all in advance.
回答1:
return fetch(config.apiUrl + "events", {
- so the fetch request is expecting the first argument to be a URL. So where config.apiUrl is defined is where the request goes. You can change this to point to the localhost instance of your server you are running.
回答2:
So I have found the issue, thought I would post it. The problem wasn't my endpoints or my NS install. It is because Android Emulators out of the box cannot reference local sites running on your dev machine without some modification to the hosts settings on the Emulator. This process is explained here but I want to emphasize for anyone having the same issue.
Android emulators do not support access to local machine sites out of the box as explained here: https://developer.android.com/studio/run/emulator-networking.html
It needs to be setup through the device EVERYTIME unless someone has a solution to set this up only once, I would love to see it shared here.
There are serveral solutions outlining similar steps but I have had issues on every single one. Here is one that outlines the gist of the issue: http://sadhanasiblog.blogspot.in/2012/07/local-environment-setup-for-android.html
Thanks for the help.
来源:https://stackoverflow.com/questions/43150805/developing-nativescript-app-with-local-api