2017-12-07 24 views
0

初めてNativescriptでアプリケーションを開発し、AJAXがAndroidでは動作しますがiOSでは動作しないという問題に直面しています。私はuser-view-model(user-view-model.js)を必要とするlogin.jsファイルを持っています。そして、Android上でコードをテストすると、私は "home"ページに移動しますが、iOS上のcatch関数に当ります。AjaxはAndroidで動作しますが、iOSでは動作しません。

login.js:

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

 
var user = new UserViewModel({ 
 
    email: "[email protected]", 
 
    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) { 
 
    dialogsModule.alert({ 
 
     message: "Unfortunately we could not find your account.", 
 
     okButtonText: "OK" 
 
    }); 
 
    return Promise.reject(); 
 
    }).then(function(response) { 
 
    console.dir(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(); 
 
    topmost.navigate("views/home/home"); 
 
    }); 
 
};

ユーザービュー-model.js:

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; 
 
     console.log(loginUrl); 
 
    
 
     // 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) { 
 
       console.dir(response); 
 
       return response.results[0]; 
 
     }) 
 

 
    }; 
 

 
    return viewModel; 
 
}; 
 

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

 
module.exports = User;

は私のコードと根本的に何か問題があり、または非同期呼び出し作業を行いますiOSとNativescriptのAndroidの違いは何ですか?私は食料品のチュートリアルを行い、この問題にぶつからなかったので、これは当てはまりませんでした。バックエンドがPHPを使用していることが重要ですか?

答えて

0

私は問題を修正しました:Angular 2の新しいプロジェクトを開始して同じエラーが発生しましたが、エラーメッセージ "エラー:App Transport Securityポリシーで使用する必要があるため、安全な接続の。私はURLの呼び出しに "https"を追加して解決しましたが、this postには別の解決策があります。

関連する問題