API在浏览器上工作,但在带有phonegap的设备上没有

时间:2013-10-22 20:57:01

标签: javascript android cordova

我正在使用phonegap开发一款应用。我正在使用edmunds API来获取汽车的照片。 API在浏览器上运行正常,但是当我在设备上运行它时,它不会显示图像。有谁知道原因?有没有办法在设备上调试javascript?

以下是代码:

 <script>
    window.sdkAsyncInit = function() {
        // Instantiate the SDK
        var res = new EDMUNDSAPI('xxxx');
        // Optional parameters
        var options = {};
        var style;
        // Callback function to be called when the API response is returned
        function success(res) {

            style = res.styles[0].id;
            console.log(style);

            showimage(style);

        };
        function showimage(style){

            var opt = {
                "styleId": ""+style+"",
                "comparator": "simple"
            };
        console.log(opt);
            res.api('/v1/api/vehiclephoto/service/findphotosbystyleid', opt, success2, fail);

        }
        function success2(res){
            var body = document.getElementById('image');
            body.innerHTML = "<img src='http://media.ed.edmunds-media.com"+ res[0].photoSrcs[0] +"' />";
        }

        function fail(data) {
            console.log(data);
        }
        // Fire the API call
        var make = localStorage.getItem('make');
        var model = localStorage.getItem('model');
        var year = localStorage.getItem('year');
        console.log(make+model+year);

        res.api('/api/vehicle/v2/'+make+'/'+model+'/'+year+'/styles', options, success, fail);
        // Additional initialization code such as adding Event Listeners goes here
  };
  // Load the SDK asynchronously
  (function(d, s, id){
        var js, sdkjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement(s); js.id = id;
        js.src = "./edmunds.api.sdk.js";
        sdkjs.parentNode.insertBefore(js, sdkjs);
   }(document, 'script', 'edmunds-jssdk'));
</script>

2 个答案:

答案 0 :(得分:3)

您可以使用:

http://debug.phonegap.com/(也称为weinre)

您是否在白名单中启用了API?

你有adb logcat吗?

答案 1 :(得分:1)

API正在从窗口获取协议,该窗口不适用于手机应用程序,将其更改为https并且有效。