User
User

Reputation: 1271

Can't Find variable in Phonegap using javascript

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>

Upvotes: 1

Views: 1932

Answers (2)

Maulik
Maulik

Reputation: 3316

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



    }

}

Upvotes: 2

Vlad Stirbu
Vlad Stirbu

Reputation: 1792

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/

Upvotes: 0

Related Questions