添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
私奔的佛珠  ·  jenkins pipeline post ...·  2 年前    · 
另类的滑板  ·  Orson welles - ...·  2 年前    · 

Ajax成功函数在android webview上不工作

3 人关注

我做了一个使用asp.net的web项目,在android web视图中加载网站。我在网站中使用了Ajax,并重定向到一个URL以获得成功。它在网页浏览器和手机浏览器上工作正常。但是在android web视图中,Ajax不工作,也没有重定向。我做了很多测试,但找不到问题所在。我添加了 return false ,它有助于重定向URL,但它不是正确的解决方案。Ajax success函数不工作。在控制台中没有错误。下面是我的代码:-

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!!!");
    
1 个评论
你是否使用远程调试工具检查了AJAX请求是否在WebView中工作? developers.google.com/web/tools/chrome-devtools/… ?
android
jquery
ajax
android-studio
acmsohail
acmsohail
发布于 2020-08-18
1 个回答
Kiran Shakya
Kiran Shakya
发布于 2021-02-23
已采纳
0 人赞同

如果你正在重定向到不同的地方,你可能不需要以编程方式点击一些按钮。在移动浏览器/webview中的代码导致的点击事件被认为是不安全的并被忽略。

另外,在移动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() {