【问题标题】:Can't Find variable in Phonegap using javascript使用javascript在Phonegap中找不到变量
【发布时间】:2013-02-15 06:41:12
【问题描述】:

当我在浏览器上运行这个程序时,我得到了失败的响应,但是在 android 手机上运行这个程序我得到参考错误找不到变量 data。代码也可以在模拟器中完美运行。帮帮我

 <html>
    <head>
    <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>

    <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
    <script type = "text/javascript">
    function getResponse(){

    var url = "http://www.apexweb.co.in/apex_quote/phone_gap/uname_validation.asp?un=9999999999&pwd=123456789&callback=your_callback";

    var head= document.getElementsByTagName('head')[0];
        var script= document.createElement('script');
        script.type= 'text/javascript';
        script.src= url;
        head.appendChild(script);

     your_callback(data);    //here i am getting reference error, can't find  variable data

     }

    function  your_callback(data){

    var st = data.status;
    alert(st);

    }


    </script>
    </head>
    <body>
    <button input type = "button" onClick = "getResponse()"> GetResponse </button>
    </body>
    </html>

【问题讨论】:

  • 我也找不到变量data =\ 应该在哪里定义?
  • 调用your_callback 时,data 未定义任何内容,因此这不足为奇。你想让your_callback 使用什么?
  • 另一件事:最佳实践是在使用函数之前定义函数 - 所以我建议将 your_callback 的定义移至 getResponse 的定义。
  • 我没有声明数据变量,我必须声明
  • 在下面查看我编辑的答案。

标签: javascript android jquery cordova jquery-mobile


【解决方案1】:

无论我从您的代码中了解到什么,我都在其中添加了一些代码,请在下面查看我的答案:

我还添加了数据变量声明并从中获取值。

<script type = "text/javascript">

   var data=""; // Declare varible here so that you can get variable in both function.
        function getResponse(){

        var url = "http://www.apexweb.co.in/apex_quote/phone_gap/uname_validation.asp?un=9999999999&pwd=123456789&callback=your_callback";

        var head= document.getElementsByTagName('head')[0];
            var script= document.createElement('script');
            script.type= 'text/javascript';
            script.src= url;
            data = head.appendChild(script); // get your script value in data variable

         your_callback(data);    //here i am getting reference error, can't find  variable data

         }

        function  your_callback(data){

        var st = data.value;
        alert('script value is: '+st);

        }


        </script>

确保您已将 html 文件放入 assest/www 文件夹和此代码:

通过使用 web 视图,您可以使用 java 脚本和 html 页面。

public class MainActivity extends Activity {

    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/index.html");
//        webView.loadUrl("WebAppDemo/assets/www/index.html");



    }

}

【讨论】:

  • 我尝试了这段代码,第一次我收到未定义的警报。它可以在浏览器和模拟器中运行,但它不能在 android 手机中运行。
  • 尝试使用 alert('script value is: ' +data.value);实际上我不知道状态的事情,但是如果你想从变量中获取值,那么你可以使用 .value 来获取(比如:+ document.getElementById('username').value)。
  • 检查此链接以获取电话间隙中的警报对话框示例,它将为您提供帮助:mobile.tutsplus.com/tutorials/phonegap/… 并检查我上面编辑的答案
【解决方案2】:

您的 url 变量的值应与 JSONP [1] 调用一起使用。有独立的帮助程序库 [2] 或 Jquery 选项 [3]。您应该使用其中之一,因为您不必手动处理所有 JSONP 机制。

我还注意到您同时包含 phonegap.js 和 cordova.js。您应该删除第一个。真的很老了。

[1]http://en.wikipedia.org/wiki/JSONP

[2]http://pastebin.com/ADxHdCnB

[3]http://api.jquery.com/jQuery.getJSON/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2019-07-21
    相关资源
    最近更新 更多