Allow remote ajax calls in Android Webview + jquery mobile phone

I am developing a javascript / HTML application with jquerymobile that makes ajax requests to a remote server. The application works fine on Chrome (only launching Chrome with website protection disabled), but when I paste it into the assets / Android directory of the application (simple web browsing), remote ajax calls fail. So, I think it might be a cross domain problem. I know that the phone book does not have this problem, but I would not want to use the handset, if possible. So the question is: how do I disable cross-domain protection in an Android web browser application?

this is the operation code:

public class Moby extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_moby); WebView mbrowser = (WebView) findViewById(R.id.webView1); //get the WebView from the layout XML if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) mbrowser.getSettings().setAllowUniversalAccessFromFileURLs(true); //mbrowser.setWebChromeClient(new WebChromeClient()); mbrowser.setWebViewClient(new WebViewClient()); mbrowser.loadUrl("file:///android_asset/index.html"); //set the HTML WebSettings settings = mbrowser.getSettings(); settings.setJavaScriptEnabled(true); } 

}

 <uses-permission android:name="android.permission.INTERNET" /> 

And I already set the jquerymobile cross domain parameters in my html pages:

 <script src="script/jquery-1.8.2.js"></script> <script> $(document).bind("mobileinit", function(){ $.support.cors = true; $.mobile.allowCrossDomainPages = true; }); </script> <script src="script/jquery.mobile-1.2.0.js"></script> 
+5
android ajax jquery-mobile cross-domain android-webview
source share
4 answers

try it

 WebView web=(WebView) findViewById(R.id.webView1); web.getSettings().setJavaScriptEnabled(true); 
+4
source share

I do not have enough points to post this as a comment, however, please read the following:

ajax works on some Android devices and not on others

In particular, Chrome-based web views require the following:

webView.getSettings () setAllowUniversalAccessFromFileURLs (true) ;.

Edit: Sorry, I just saw that you are already doing this - I tested it at my end and it seems to have solved my problem (I got an error with access-source-source control when loading local links via ajax).

+2
source share

In your AndroidManifest.xml you have this line:

 <uses-permission android:name="android.permission.INTERNET" /> 
0
source share

I had the same problem and fixed it by overriding ifInterceptRequest in my WebViewClient. I intercept the ajax call and do it in java. You must do the same for the POST methods.

 private class MyWebViewClient extends WebViewClient { @Override public void onPageFinished(WebView webView, String url) { Log.d("test", "onPageFinished"); loadWebViewJavascriptBridgeJs(webView); } @Override public WebResourceResponse shouldInterceptRequest(WebView webview, WebResourceRequest webrequest) { Log.d("test", "shouldInterceptRequest"); return this.handleRequest(webrequest.getUrl().toString()); } @NonNull private WebResourceResponse handleRequest(@NonNull String urlString) { try { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestProperty("User-Agent", ""); connection.setRequestMethod("GET"); connection.setDoInput(true); connection.connect(); InputStream inputStream = connection.getInputStream(); return new WebResourceResponse("text/json", "utf-8", inputStream); } catch (MalformedURLException e) { e.printStackTrace(); return null; } catch (ProtocolException e) { e.printStackTrace(); return null; }catch (IOException e) { e.printStackTrace(); return null; } } } 

When I talk about my webview, I launch my WebViewClient

  mWebView.setWebViewClient(new MyWebViewClient()); 
0
source share

All Articles