1

当我在浏览器上运行此程序时,我收到失败的响应,但在 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>
4

2 回答 2

2

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

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

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



    }

}
于 2013-02-15T07:43:39.563 回答
0

您的 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/

于 2013-02-15T07:45:05.870 回答