【问题标题】:Ajax response not appears inside success function - jquery mobileAjax响应没有出现在成功函数中 - jquery mobile
【发布时间】:2016-03-25 21:05:58
【问题描述】:

我正在尝试使用 jquery mobile 中的用户名、密码字段的简单登录表单。用户名和密码应该从 ajax 页面验证。在我的系统中,我能够完美地得到响应。将我的代码转换为 .apk uging phonegap 时,我的手机无法接收来自 ajax 页面的响应。成功函数中的任何代码都不起作用,直接进入错误函数。我究竟做错了什么?

    $(document).on('pagebeforeshow', '#login', function(){
        $(document).on('click', '#submit', function() {
        if($('#username').val().length > 0 && $('#password').val().length > 0){
            $.ajax({
                url : 'liveurl/check.php',
                data: {action : 'login', formData : $('#check-user').serialize()}, 
                type: 'post',
                beforeSend: function() {
                     $.mobile.loading(true);                 },
                complete: function() {
                     $.mobile.loading(false); 
                },
                success: function (result) {
                   if(result.status == "success"){  
                        resultObject.formSubmitionResult = result.uname;
                        localStorage["login_details"] = window.JSON.stringify(result);
                        $.mobile.changePage("#second");
                }else{
                        $.mobile.changePage("#login");
                        alert("incorrect login");           
                }
                    },
                    error: function (request,error) {
                        alert(error);    
                    }
                });                   
        } else {
            alert('Fill all nececery fields');
        }           
            return false;
        });
});

【问题讨论】:

  • 你在 liveurl 上使用 https 吗?你能解释一下“在我的系统中,我能够完美地得到响应”是什么意思吗?
  • 是的,我只使用 http。我猜 phonegap 无法响应 http url。
  • 您能解释一下“在我的系统中,我能够完美地得到响应”是什么意思吗?
  • 我的代码在我的桌面上完美运行。当将其转换为 .apk 并在手机(phonegap)中使用时,它不起作用。没有回应什么都没有。
  • 那么可能是白名单问题,你必须安装白名单插件并配置它允许外部连接。 npmjs.com/package/cordova-plugin-whitelist

标签: javascript ajax cordova jquery-mobile


【解决方案1】:

两点:

  1. 您的 APK 未在您的服务器上运行。这意味着,你的网址是错误的,它需要是这样的:

网址:“http://www.your_server.com/liveurl/check.php

  1. 您必须将每个外部网址加入白名单,请阅读相关文档:

http://cordova.apache.org/docs/en/dev/guide/appdev/whitelist/index.html

【讨论】:

  • 感谢您的回复。例如,只有我记为“liveurl/check.php”。网址格式如您所说。现在我只需要专注于白名单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
相关资源
最近更新 更多