android - Ajax成功功能在android webview上不起作用
问题描述
我使用 asp.net 做了一个 web 项目,并在 android web 视图中加载了网站。我在网站中使用了 Ajax 并重定向到一个 URL 以获得成功。它在网络浏览器和移动浏览器上运行良好。但是在 android web view 中,ajax 不起作用并且它没有被重定向。我做了很多测试,找不到问题。我补充return false
说它有助于重定向 URL,但这不是正确的解决方案。Ajax 成功功能不起作用。控制台没有错误。这是我的代码:-
package com.project.projectname;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Home extends AppCompatActivity {
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
webview = (WebView) findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:") || url.startsWith("https://www.facebook.com/sharer") || url.startsWith("http://twitter.com/share") || url.startsWith("https://wa.me/") || url.startsWith("https://plus.google.com/share")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
view.reload();
return true;
}
view.loadUrl(url);
return true;
//return false; This is redirect but ajax success function is not working
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
// webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webview.getSettings().setAppCacheEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setSavePassword(true);
webview.getSettings().setSaveFormData(true);
webview.getSettings().setEnableSmoothTransition(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("your website URL");
}
@Override
public void onBackPressed() {
if (webview.isFocused() && webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
finish();
}
}
}
网站中的 Ajax 代码
$.ajax({
url: '/tapaking/Checkout/Details',
type: "POST",
data: JSON.stringify({
cart: json2,
note: note,
schdate: schdate,
schtime: schtime,
payment: payment
}),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(response) {
if (response.success) {
$('.sc-cart-clear').click();
window.location.href = "https://yourwebsite.com/User/UserProfile";
} else {
}
},
error: function() {
//alert("An error has occured!!!");
}
});
解决方案
如果您要重定向到不同的地方,您可能不需要以编程方式单击某些按钮。由于移动浏览器/网络视图中的代码导致的点击事件被认为是不安全的并被忽略。
此外,在移动 webview 的情况下,我们使用document.location
. window.location.href
我认为您的代码中的这些细微更改会有所帮助:
$.ajax({
url: '/tapaking/Checkout/Details',
type: "POST",
data: JSON.stringify({
cart: json2,
note: note,
schdate: schdate,
schtime: schtime,
payment: payment
}),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(response) {
if (response.success) {
// Probably we don't need to click something here.
// If required, we can clear with direct invocation of clear action.
// $('.sc-cart-clear').click();
document.location = "https://yourwebsite.com/User/UserProfile";
} else {
}
},
error: function() {
//alert("An error has occured!!!");
}
});
如果document.location
不起作用,也可以尝试document.location.replace(url)
。
推荐阅读
- java - Gradle 将哪些文件打包到 jar 存档中?
- xcode - 当我调用 swiftUI 视图时,Xcode SwiftUI 与 ViewController 混合,导航栏出现带有后退按钮,单击后如何将其删除
- ffmpeg - FFmpeg - 捕获一个 rtsp 流并将其重新流式传输到另一个 rtsp-server
- .net - HttpListener 认证
- css - CSS仅更改表格中没有i列的一行的属性
- delphi - 将字符串填充到固定列宽
- laravel - 如何将参数传递给模拟类构造函数
- opencv - 如何将 System.drawing.pointF 转换为 OpenCvSharp.Point
- javascript - 显示 Map 中的数组元素
- java - 未找到用于签名配置“发布”颤振的密钥库文件