使用JavaScript中的PhoneGap找不到变量 [英] Can't Find variable in Phonegap using javascript

查看:184
本文介绍了使用JavaScript中的PhoneGap找不到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在浏览器中我得到响应,无法运行此程序,而是运行在这个Android手机我收到引用错误找不到变量数据即可。 code在模拟器完美的作品也。帮助我

 < HTML和GT;
    < HEAD>
    <脚本类型=文/ JavaScript的字符集=utf-8SRC =PhoneGap的-1.0.0.js>< / SCRIPT>    <脚本类型=文/ JavaScript的字符集=utf-8SRC =科尔多瓦-2.4.0.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的>
    功能的GetResponse(){    VAR URL = \"http://www.apexweb.co.in/apex_quote/phone_gap/uname_validation.asp?un=9999999999&pwd=123456789&callback=your_callback\";    VAR头= document.getElementsByTagName('头')[0];
        VAR脚本=使用document.createElement('脚本');
        script.type =文/ JavaScript的';
        script.src =网址;
        head.appendChild(脚本);     your_callback(数据); //这里我得到引用错误,找不到可变数据     }    功能your_callback(数据){    VAR ST = data.status;
    警示(ST);    }
    < / SCRIPT>
    < /头>
    <身体GT;
    <按钮输入类型=按钮的onClick =GETRESPONSE()>的GetResponse< /按钮>
    < /身体GT;
    < / HTML>


解决方案

无论我从你的code明白我已经添加了一些code的话,请检查我的回答如下:

我还增加了数据变量声明,并从中获得价值。

 <脚本类型=文/ JavaScript的>   VAR数据=; //声明varible在这里,让您可以在这两个函数来获取变量。
        功能的GetResponse(){        VAR URL = \"http://www.apexweb.co.in/apex_quote/phone_gap/uname_validation.asp?un=9999999999&pwd=123456789&callback=your_callback\";        VAR头= document.getElementsByTagName('头')[0];
            VAR脚本=使用document.createElement('脚本');
            script.type =文/ JavaScript的';
            script.src =网址;
            数据= head.appendChild(脚本); //获取数据变量脚本值         your_callback(数据); //这里我得到引用错误,找不到可变数据         }        功能your_callback(数据){        VAR ST = data.value;
        警报('script值是:'+ ST);        }
        < / SCRIPT>

请确保你已经把HTML文件中入资产/ www文件夹和放大器;这code:

通过使用Web视图,您可以使用Java脚本和HTML页面。

 公共类MainActivity延伸活动{    web视图的WebView;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        web视图=(的WebView)findViewById(R.id.webView);
        webView.getSettings()setJavaScriptEnabled(真)。
        webView.setWebChromeClient(新WebChromeClient());
        webView.loadUrl(文件:///android_asset/www/index.html);
// webView.loadUrl(WebAppDemo /资产/ WWW / index.html的);    }}

when i run this program on browser i am getting response as fail, but run this in android phone i am getting reference error can't find variable data. Code perfectly works in emulator also. Help Me

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

解决方案

WhatEver I understood from your code I have added some code in it, please check my answer below:

I have also added data variable declaration and get value from it.

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

Make sure that you have put html file in assest/www folder & this code:

By using web view you can use java script and html page.

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



    }

}

这篇关于使用JavaScript中的PhoneGap找不到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆