Wednesday, 17 February 2021

Ajax success function is not working on android webview

I did a web project using asp.net and loading the website in the android web view. I have used Ajax in the website and redirect to a URL for success. It's working fine on the web browser and the mobile browser. But in android web view ajax is not working and it's not redirected. I did lots of testing and couldn't find the issue. I added return false it helps to redirect the URL but it's not the correct solution. Ajax success function is not working. No error in the console. Here is my code:-

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 code in the website

$.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!!!");
    }
});


from Ajax success function is not working on android webview

No comments:

Post a Comment