private void initView() { LinearLayout llRoot = findViewById(R.id.ll_root); wvShow = new WebView(getApplicationContext()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); wvShow.setBackgroundColor(ContextCompat.getColor(this, R.color.white)); llRoot.addView(wvShow,params); } private void setupView() { WebSettings settings = wvShow.getSettings(); settings.setJavaScriptEnabled(true); settings.setAllowUniversalAccessFromFileURLs(true); settings.setDefaultTextEncodingName("utf-8"); String url = ""; wvShow.loadUrl(url); } @Override protected void onDestroy() { if (wvShow != null) { ViewParent viewParent = wvShow.getParent(); if (viewParent != null){ ((ViewGroup)viewParent).removeView(wvShow); } wvShow.stopLoading(); wvShow.getSettings().setJavaScriptEnabled(false); wvShow.clearHistory(); wvShow.removeAllViews(); wvShow.destroy(); } super.onDestroy(); }
https://www.jianshu.com/p/3e8f7dbb0dc7javascript
報錯:XMLHttpRequest cannot load http://www.xx.com/xx. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'null' is therefore not allowed access."
Android端:php
WebSettings settings = wvShow.getSettings(); settings.setJavaScriptEnabled(true); settings.setAllowUniversalAccessFromFileURLs(true); // 跨域 settings.setDefaultTextEncodingName("utf-8"); wvShow.loadUrl("http://www.xx.com/");
ajax請求html
$.ajax({ url : path, type: "POST", crossDomain: true, // 跨域配置 xhrFields: { withCredentials: true //發送憑據 }, success : function(result) { alert("success"); } });
php 容許全部域名訪問java
header('Access-Control-Allow-Origin:*'); //跨域訪問 header("Access-Control-Allow-Credentials: true"); //容許跨域請求中攜帶cookie // 服務器端 Access-Control-Allow-Credentials = true時,參數Access-Control-Allow-Origin 的值不能爲 '*'
參考:android
webview跨域問題解決方案 http://blog.csdn.net/yclfdn2004/article/details/51364660git
跨域Ajax請求時是否帶Cookie的設置 http://blog.csdn.net/wzl002/article/details/51441704github
先後端分離的web項目,ajax跨域請求後端攜帶cookie https://my.oschina.net/qinghang/blog/1608792web
http://blog.hellofe.com/web/2014/12/28/the-CORS-protocol/ajax
/** * webview加載網頁url */ private void LoadAuthPageUrl() { String url = "xxxxxx"; wbAuth.setWebChromeClient(new WebChromeClient(){ [@Override](https://my.oschina.net/u/1162528) public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); Log.d(TAG, "==" + title); } }); wbAuth.setWebViewClient(new WebViewClient(){ [@Override](https://my.oschina.net/u/1162528) public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); view.loadUrl("javascript:(function() { " + jsFormInjectCode + "})()"); //注入javascript } [@Override](https://my.oschina.net/u/1162528) public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } [@Override](https://my.oschina.net/u/1162528) public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); Log.d(TAG, "==" +errorCode + "=" + description + "=" +failingUrl); } }); // 將FormDataInterface類中的processFormData方法映射到javascript中的window.FORMOUT.processFormData(data); wbAuth.addJavascriptInterface(new FormDataInterface(), "FORMOUT"); WebSettings settings = wbAuth.getSettings(); settings.setJavaScriptEnabled(true); // 支持javascript settings.setDefaultTextEncodingName("utf-8"); wbAuth.loadUrl(url); // 加載網頁url //wbAuth.loadDataWithBaseURL(url, data, "text/html", "utf-8", null); // 直接加載html字符串,第一個url是html種資源獲取的根路徑 } /** * javascript的交互類 */ private class FormDataInterface { @JavascriptInterface public void processFormData(String formData) { // 獲取到的值: // method=post&action=xx&response_type=code&redirect_uri=xxx&username=xxx&password=xxx Log.d(TAG, "====" + formData); } } // 注入的javascript代碼 String jsFormInjectCode = "function parseForm(event) {" + " var form = this;" + " if (this.tagName.toLowerCase() != 'form')" + " form = this.form;" + " var data = '';" + " if (!form.method) form.method = 'get';" + " data += 'method=' + form.method;" + " data += '&action=' + form.action;" + " var inputs = document.forms[0].getElementsByTagName('input');" + " for (var i = 0; i < inputs.length; i++) {" + " var field = inputs[i];" + " if (field.type != 'submit' && field.type != 'reset' && field.type != 'button')" + " data += '&' + field.name + '=' + field.value;" + " }" + " window.FORMOUT.processFormData(data);" + "}" + "" + "for (var form_idx = 0; form_idx < document.forms.length; ++form_idx)" + " document.forms[form_idx].addEventListener('submit', parseForm, false);" + "var inputs = document.getElementsByTagName('input');" + "for (var i = 0; i < inputs.length; i++) {" + " if (inputs[i].getAttribute('type') == 'button')" + " inputs[i].addEventListener('click', parseForm, false);" + "}";