1

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>
5
  • I can't find the variable data either =\ Where is it supposed to be defined? Commented Feb 15, 2013 at 7:13
  • when calling your_callback, data is not defined to anything, so that should not be surprising. What do you want your_callback to work with? Commented Feb 15, 2013 at 7:14
  • Antoher thing: Best practice is to define functions before using them - so I would recoment moving the definition of your_callback abpve the definition of getResponse. Commented Feb 15, 2013 at 7:15
  • i am not declare data variable, where i have to declare Commented Feb 15, 2013 at 7:19
  • check my edited answer below. Commented Feb 15, 2013 at 11:36

2 Answers 2

2

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



    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

i try this code, first time i am getting alert as undefined. It's working in browser and emulator, but y it's nt working in android mobile.
Try using alert('script value is: ' +data.value); actually I don't know about status thing but If you want to get value from variable then you can get by using .value (Like: + document.getElementById('username').value).
check this link for alert dialog example in phone gap it will help you: mobile.tutsplus.com/tutorials/phonegap/… and also check my edited answer above
0

The value of your url variable should be used with a JSONP [1] call. There are stand alone helper libraries [2] or the Jquery option [3]. You should use one of them, as you don't have to do manually with all JSONP mechanics.

I noticed also that you include both phonegap.js and cordova.js. You should remove the first. It is really old.

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.