可以't使用javascript在Phonegap中查找变量

Can't Find variable in Phonegap using javascript

本文关键字:Phonegap 查找 变量 javascript 使用 可以      更新时间:2023-09-26

当我在浏览器上运行此程序时,我得到的响应为失败,但在android手机中运行此程序,我得到参考错误,找不到变量数据。代码在模拟器中也能完美工作。帮助我

 <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>

我从你的代码中了解到了什么,我在其中添加了一些代码,请检查下面的答案:

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

<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");

    }
}

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/