Home Ajax calls working in Android but not iOS
Reply: 1

Ajax calls working in Android but not iOS

Brian Lin
Brian Lin Published in 2017-12-07 15:59:29Z

I'm developing an app in Nativescript for the first time and running into an issue where AJAX calls work on Android but not iOS. I have a login.js file which requires a user-view-model (user-view-model.js), and when I test the code on Android it takes me to the "home" page but it hits the catch function on iOS.


var dialogsModule = require("ui/dialogs");
var UserViewModel = require("../../shared/view-models/user-view-model");
var applicationSettings = require("application-settings");

var user = new UserViewModel({ 
  email: "aaa@aaa.com", 
  password: "aaa" 

var frameModule = require("ui/frame");
var page;

exports.loaded = function(args) {
  page = args.object;
  page.bindingContext = user;

exports.login = function () {
  user.login().catch(function(error) {
      message: "Unfortunately we could not find your account.",
      okButtonText: "OK"
    return Promise.reject();
  }).then(function(response) {
    console.log("past response")
    applicationSettings.setString("user_id", response.user_id);
    applicationSettings.setString("first_name", response.first_name);
    applicationSettings.setString("last_name", response.last_name);
    applicationSettings.setString("user_type", response.user_type);
    var topmost = frameModule.topmost();


var config = require("../../shared/config");
var fetchModule = require("fetch");
var observableModule = require("data/observable");
var http = require("http");

function User(info) {
    info = info || {};

    var viewModel = new observableModule.fromObject({
        email: info.email || "",
        password: info.password || ""

    viewModel.login = function() {
        let loginEmail = JSON.stringify(this.get("email")).replace(/['"]+/g, '');
        let loginPassword = JSON.stringify(this.get("password")).replace(/['"]+/g, '');
        console.log(loginEmail, loginPassword);
        let loginUrl = config.serverPHPServiceUrl + "Login.php?user_id=" + loginEmail + "&password=" + loginPassword;
        // I tried this way first and wasn't able to login on iOS, which made me try the second method below.
        // return fetchModule.fetch(loginUrl, {
        //   method: "POST",
        //   headers: {
        //     "Content-Type": "application/json"
        //   }
        // }).then(handleErrors).then(function(response) {
        //   return response.json();
        // }).then(function(data) {
        //   console.dir(data);
        //   console.log(data["results"][0]["user_id"])
        //   return data["results"][0];
        // });

        // This method works on Android but not iOS.
        return http.getJSON(loginUrl).then(function(response) {
              return response.results[0];


    return viewModel;

function handleErrors(response) {
    console.log("in errors")
    if (!response.ok) {
        throw Error(response.statusText);
    return response;

module.exports = User;

Is there anything fundamentally wrong with my code, or do asynchronous calls work differently on iOS vs Android in Nativescript? I did the Grocery tutorial and didn't run into this issue, so I didn't think this was the case. Does it matter that the backend is using PHP?

Brian Lin
Brian Lin Reply to 2017-12-08 00:07:03Z

I fixed my issue: I started a new project with Angular 2 and ran into the same error, but then it gave me the error message "Error: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." I solved it by adding "https" to my url call, but this post has another solution.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311642 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO