【问题标题】:ajax call not working in android application using phonegapajax调用在使用phonegap的android应用程序中不起作用
【发布时间】:2015-07-08 14:22:26
【问题描述】:

我正在使用 phonegap 为 android 应用程序创建 apk 文件。下面是我index.html的代码

<html>
<head><title>Test application</title>
    <script src="Scripts/jquery.js"></script>
    <script>
        $(document).ready(function(){
            $("#submitValue").on("click",function(){
                alert("");
                getServerResponse($("#userValue").val());
            });
        });

        function getServerResponse(userValue){
            var serverUrl="http://xxx.site.net/xxx/Hello/"+ userValue +"?format=json";
                $.ajax({
                    url:serverUrl,
                    type:"GET",
                    contentType:"application/json",
                    dataType:"jsonp",

                    success:function(data){
                        alert("Success");
                        $("#serverResponse").html(data.Result);
                    },

                    error:function(){
                        alert("error");
                    }
                });
            };
    </script>
</head>
<body>
    <div>
        <h3>Enter your name and click submit</h3>
    </div>

    <div>
        <input type="text" placeholder="Enter your Name" id="userValue"/>
        <input type="button" id="submitValue" value="Submit"/>
    </div>

    <div id="serverResponse"></div>
</body>

当我使用浏览器打开时,这非常有效,但是当打开应用程序并单击提交时,我会收到单击警报,但之后没有任何反应。当我通过 android 应用单击提交按钮时,既没有成功警报也没有错误警报

【问题讨论】:

  • 可能是跨域请求?查看控制台,看看网络中发生了什么
  • @BipBip 在任何浏览器中都可以正常工作,并且仅适用于跨源请求我添加了 dataType:"jsonp" 只有当它是一个 android 应用程序时才会出现问题
  • 在您的 config.xml 中,是否有这样的一行

标签: android jquery ajax cordova phonegap-plugins


【解决方案1】:

您需要在 AndroidManifest.xml 文件中授予 INTERNET 权限。 在 AndroidManifest.xml 中包含以下行

<uses-permission android:name="android.permission.INTERNET"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多