2016-08-29 83 views
0

我想從android webview調用ajax,但它不工作。我在控制檯窗口中收到此錯誤Ajax不能在webview中工作android

08-29 12:28:16.411: E/CONTENT(14146): [object Object] @ 1: http://loconav.com/assets/m/history_selector-1b620f001e31e9dfeff6981b3ee9b290.js 
08-29 12:28:16.536: E/CONTENT(14146): Uncaught TypeError: undefined is not a function @ 1: http://loconav.com/assets/m/history_selector-1b620f001e31e9dfeff6981b3ee9b290.js 
08-29 12:28:16.683: E/CONTENT(14146): Uncaught TypeError: undefined is not a function @ 1: http://loconav.com/assets/m/history_selector-1b620f001e31e9dfeff6981b3ee9b290.js 
08-29 12:28:16.883: E/CONTENT(14146): Uncaught TypeError: undefined is not a function @ 1: http://loconav.com/assets/m/history_selector-1b620f001e31e9dfeff6981b3ee9b290.js 
08-29 12:28:17.202: E/CONTENT(14146): Uncaught TypeError: undefined is not a function @ 1: http://loconav.com/assets/m/history_selector-1b620f001e31e9dfeff6981b3ee9b290.js 

我使用這個Android代碼

webview = (WebView) findViewById(R.id.webview); 
     webview.setWebViewClient(new WebViewClient(){ 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) 
      { 
       return super.shouldOverrideUrlLoading(view, url); 
      } 

      public void onPageStarted(WebView view, String url, Bitmap favicon) { 
       dialog.show(); 
      } 

      @Override 
      public void onPageFinished(WebView view, String url) { 
       if(url.equals("http://xyz")){ 
        editor.putString("login", "true"); 
        editor.commit(); 
        trytologin = false; 
       }else if(url.equals("http://xyz")){ 
        editor.clear(); 
        editor.commit(); 
        Intent intent = new Intent(WebActivity.this,MainActivity.class); 
        if(trytologin == true){ 
         intent.putExtra("login", "failed"); 
        } 
        startActivity(intent); 
        finish(); 
       } 
       if(!isNetworkAvailable()){ 
        alertDialog(); 
        linearLayout.setVisibility(View.VISIBLE); 
        webview.setVisibility(View.GONE); 
       }else{ 
        webview.setVisibility(View.VISIBLE); 
        imageView.setVisibility(View.VISIBLE); 
       } 
       dialog.dismiss(); 
       //pd.dismiss(); 
       //Toast.makeText(WebActivity.this, url, Toast.LENGTH_LONG).show(); 
      } 
     }); 
     webview.setWebChromeClient(new WebChromeClient(){ 
      @Override 
       public boolean onConsoleMessage(ConsoleMessage cm) 
       { 
        Log.e("CONTENT", String.format("%s @ %d: %s", 
           cm.message(), cm.lineNumber(), cm.sourceId())); 
        return true; 
       } 
     }); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.getSettings().setAppCacheEnabled(true); 
     webview.getSettings().setDatabaseEnabled(true); 
     webview.getSettings().setDomStorageEnabled(true); 
     webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
     webview.getSettings().setSupportZoom(true); 
     webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     webview.getSettings().setAllowFileAccessFromFileURLs(true); 
     webview.getSettings().setAllowUniversalAccessFromFileURLs(true); 
     webview.getSettings().setLoadsImagesAutomatically(true); 
     webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     webview.getSettings().setLoadWithOverviewMode(true); 
     webview.getSettings().setAllowContentAccess(true); 
     webview.getSettings().setUseWideViewPort(true); 

我試圖把這個AJAX文件

function GoogleFetchAndFillHistoryLocation(t,e,n,o){$.ajax({url:"http://maps.googleapis.com/maps/api/geocode/json?latlng="+t+","+e,success:function(t){var e=[],c="",r="";t.results[0].address_components.forEach(function(t,n,o){"Unnamed Road"!=t.long_name&&e.push(t.long_name),t.types.includes("sublocality")&&(c=t.long_name),t.types.includes("locality")&&(r=t.long_name)}),console.log(t.results[0].formatted_address);var i=e.join(", "),a=c+", "+r;o.html(i),n.html(a)},error:function(t){o.html("Some error occurred while fetching address")}})}function HistorySelector(t){this.$selector=t}!function(t){"use strict";var e=function(t,n){var o=/[^\w\-\.:]/.test(t)?new Function(e.arg+",tmpl","var _e=tmpl.encode"+e.helper+",_s='"+t.replace(e.regexp,e.func)+"';return _s;"):e.cache[t]=e.cache[t]||e(e.load(t));return n?o(n,e):function(t){return o(t,e)}};e.cache={},e.load=function(t){return document.getElementById(t).innerHTML},e.regexp=/([\s'\\])(?!(?:[^{]|\{(?!%))*%\})|(?:\{%(=|#)([\s\S]+?)%\})|(\{%)|(%\})/g,e.func=function(t,e,n,o,c,r){return e?{"\n":"\\n","\r":"\\r"," ":"\\t"," ":" "}[e]||"\\"+e:n?"="===n?"'+_e("+o+")+'":"'+("+o+"==null?'':"+o+")+'":c?"';":r?"_s+='":void 0},e.encReg=/[<>&"'\x00]/g,e.encMap={"<":"&lt;",">":"&gt;","&":"&amp;",'"':"&quot;","'":"&#39;"},e.encode=function(t){return(null==t?"":""+t).replace(e.encReg,function(t){return e.encMap[t]||""})},e.arg="o",e.helper=",print=function(s,e){_s+=e?(s==null?'':s):_e(s);},include=function(s,d){_s+=tmpl(s,d);}","function"==typeof define&&define.amd?define(function(){return e}):t.tmpl=e}(this),HistorySelector.prototype.bindEvents=function(){var t=this;this.$selector.on("change",function(){t.fetchData()})},HistorySelector.prototype.fetchData=function(){$.ajax({context:this,url:this.$selector.data("url"),data:{profiles:{interval:this.$selector.val()}},headers:{"X-Auth-Token":$("#auth-token").data("token")},success:function(t){console.log(t),this.populateData(t),$(".location-panel").each(function(t,e){var n=$(e);setTimeout(function(){GoogleFetchAndFillHistoryLocation(n.data("lat"),n.data("long"),n.find(".short-location"),n.find(".formatted-address"))},200*t)})},error:function(t){console.log(t)}})},HistorySelector.prototype.populateData=function(t){var e=$(tmpl("history-locations",t).trim());$(".history-locations").empty().append(e),$("body").scrollTop()<180&&$("html, body").animate({scrollTop:"180vh"},"slow")},HistorySelector.prototype.init=function(){},$(function(){$(".selectors input").each(function(){var t=new HistorySelector($(this));t.init(),t.bindEvents()})}); 

你可以看到AJAX文件此鏈接 - http://www.loconav.com/assets/m/history_selector-1b620f001e31e9dfeff6981b3ee9b290.js

我使用的是此代碼

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>Babatrucks</title> 
    <meta content="authenticity_token" name="csrf-param" /> 
<meta content="lNJn5/GLbnS0cgUYjalFUT+UsgCcDckhWHcqI32OeJ0=" name="csrf-token" /> 

    <link href="/assets/application-33b3164661d3b2d630f9cbc88419feb4.css" media="all" rel="stylesheet" /> 
    <link href="/assets/m_application-9613cb73f45d486ef514a74fa1964a46.css" media="all" rel="stylesheet" /> 

    <script src="/assets/application-95d8b0297f927b393dd0e1aaf163bc4d.js"></script> 
    </head> 
    <body> 

    <div class="container"> 
     <div class="row"> 
     <div class="col-xs-12"> 
      <link href="/assets/m/trucks/show-bcd1a590d01e03df2a78b4c5dbd1a975.css" media="screen" rel="stylesheet" /> 
<script src="/assets/m/history_selector-1b620f001e31e9dfeff6981b3ee9b290.js"></script> 
<div class="row top-bar"> 
    <div class="col-xs-2 text-center"> 
    <a class="back-arrow" href="/m/trucks"> 
     <i class="fa fa-chevron-left" aria-hidden="true"></i> 
</a> </div> 
    <div class="col-xs-8 text-center title"> 
    NL 01L 2273 
    </div> 
    <div class="col-xs-2 text-right"> 
    </div> 
</div> 
<div class="row"> 
    <iframe class='locate-map' src="/m/trucks/296/locate"></iframe> 
    <div class="col-xs-12"> 
    <div class="row"> 
     <div class="selectors rohan-container"> 
     <ul class="backline"> 
      <hr class="rohan-hr"> 
      <li> 
      <label for="profiles_interval_1">1 hours</label> 
      <!-- <input type="radio" id="f-option" name="selector"> --> 
      <input data-url="http://www.example.com/" id="profiles_interval_1" name="profiles[interval]" type="radio" value="1" /> 
      <div class="check"></div> 
      </li> 

      <li> 
      <label for="profiles_interval_6">6 hours</label> 
      <!-- <input type="radio" id="s-option" name="selector"> --> 
      <input data-url="http://www.example.com/" id="profiles_interval_6" name="profiles[interval]" type="radio" value="6" /> 
      <div class="check"> 
       <div class="inside"></div> 
      </div> 
      </li> 

      <li> 
      <label for="profiles_interval_24">24 hours</label> 
      <!-- <input type="radio" id="t-option" name="selector"> --> 
      <input data-url="http://www.example.com/" id="profiles_interval_24" name="profiles[interval]" type="radio" value="24" /> 
      <div class="check"> 
       <div class="inside"></div> 
      </div> 
      </li> 
     </ul> 
     </div> 
     <!-- rohan ends --> 
    </div> 
    <div class="row history"> 
     <div class="col-xs-12"> 
     <div class="row"> 
      <div class="history-locations col-xs-12"></div> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 
<div class="hidden" id="auth-token" data-token=""></div> 
<script src="/assets/timeline/modernizr-04ac4151e53119ba85cdc7679379cd0d.js"></script> 
<script type="text/x-tmpl" id="history-locations"> 
    <section id="cd-timeline" class="cd-container"> 
    {% for (var i = 0; i < o['profiles'].length; i++) { %} 
     <div class="cd-timeline-block location-panel" data-lat="{%= o['profiles'][i]['lat'] %}" data-long="{%= o['profiles'][i]['long'] %}"> 
     <div class="cd-timeline-img cd-picture"> 
      <img alt="Picture" src="/assets/timeline/cd-icon-location-20955b176b47169a313e3c8c11803cc1.svg" /> 
     </div> <!-- cd-timeline-img --> 

     <div class="cd-timeline-content"> 
      <h2 class="">{%= o['profiles'][i]['actual_received_at'] %}</h2> 
      <p class="formatted-address"></p> 
     </div> <!-- cd-timeline-content --> 
     </div> <!-- cd-timeline-block --> 
    {% } %} 
    </section> 
</script> 
<script src="/assets/timeline/main-f1dcfd5d7be80e6258f34c7dded2ee86.js"></script> 


<script type="text/javascript"> 
    function FetchAndFillLocation(lat, long, infoDiv, truckNumber, speed) { 
    var request = new XMLHttpRequest(); 
    var method = 'GET'; 
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' 
       + lat 
       + ',' 
       + long 
       + '&sensor=true'; 
    var async = true; 
    request.open(method, url, async); 
    request.onreadystatechange = function(){ 
     if(request.readyState == 4 && request.status == 200){ 
     var data = JSON.parse(request.responseText); 
     var address = data.results[0]; 
     infoDiv.empty(); 
     infoDiv.append('<div class="info-box-title"><span class="left-align-number"><i class="fa fa-truck"></i> '+ truckNumber +'</span><span class="right-align-speed"><i class="fa fa-tachometer"></i> ' + speed + ' Km/h</span></div><div class="info-box-text">' + address.formatted_address + '</div>'); 
     } 
    }; 
    request.send(); 
    } 

    function UpdateMarkerPosition(current_location_div) { 
    var request = new XMLHttpRequest(); 
    var method = 'GET'; 
    var url = '/api/v1/trucks/location?truck[slug]=abc'; 
    var async = true; 
    request.open(method, url, async); 
    request.setRequestHeader('X-Auth-Token', 'abc'); 
    request.onreadystatechange = function(){ 
     if(request.readyState == 4){ 
     if(request.status == 200) { 
      var data = JSON.parse(request.responseText).current_cordinate; 
      var truckCords = { lat: data.lat, lng: data.long } 
      FetchAndFillLocation(truckCords.lat, truckCords.lng, current_location_div, data.key, data.speed); 
     } 
     setTimeout(function() { 
      UpdateMarkerPosition(current_location_div) 
     }, 10000); 
     } 
    }; 
    request.send(); 
    } 
    $(function() { 
    UpdateMarkerPosition($('.current-location')); 
    }) 
</script> 
     </div> 
     </div><!--/row--> 

     <footer> 
     </footer> 

    </div> <!-- /container --> 

    <script> 
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); 

    ga('create', 'UA-81486803-1', 'auto'); 
    ga('send', 'pageview'); 
</script> 
    </body> 
</html> 

我該如何解決錯誤感謝我提前

回答

0

我假設,即許多單個文件的不以分號結束他們的代碼。大多數jQuery腳本以(jQuery)結尾,您需要有(jQuery);
考慮在本地添加jquery或通過​​添加。