Ajax Does Not Move in WebView at API Level 10

Asked 2 years ago, Updated 2 years ago, 94 views

Even if you write the following ajax processing in javascript in the WebView of Android 2.3.3 (API level 10),
It doesn't seem to work, but is there any solution?

$.ajax({
            url —https://xxxxxxxx,
            type: "POST",
            timeout: 10000,
            cache: false,
            data: {data:xxxx},
       beforeSend: function(xhr){
              var credentials = $.base64.encode(basic_user+":"+basic_password);
              xhr.setRequestHeader("Authorization", "Basic" + credentials);
            },
            success:function(result){
                $("#result").html(result);
            }
    })

I would like to return HTML in response from the server.

addition:

Internet permissions are included.

The logs are as follows:

Failed to load resource: the server responded with a status of 401 (Authorization Required) https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Failed to load resource—Origin null is not allowed by Access-Control-Allow-Origin.https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
XMLHttpRequest cannot load https://xxxxxxxxxxxx.Origin null is not allowed by Access-Control-Allow-Origin.index.html:1

The basic authentication didn't seem to work well, so I removed the basic authentication once, but
I just didn't get a 401 error, but it didn't work...

android webview ajax

2022-09-30 18:52

1 Answers

The answer to Access-Control-Allow-Origin Error At Android 4.1 has a solution that can be used with the Android version mentioned.

This is @johnnybgoode's method.

 if(Build.VERSION.SDK_INT>=16){
    Class<?>clazz=webView.getSettings().getClass();
    Method method = null;
    try{
        method=clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);
    } catch(NoSuchMethodExceptione){
        e.printStackTrace();
    }
    if(method!=null){
        try{
            method.invoke(webView.getSettings(), true);
        } catch(IllegalAccessExceptione){
            e.printStackTrace();
        } catch(InvocationTargetException){
            e.printStackTrace();
        }
    }
}

As this is a reflection process, please pay attention to the setting if you have included ProGuard.
Please change the try_catch according to your IDE.

Failed to load resource: Failed to load resource: Origin null is not allowed by Access-Control-Allow-Origin is a restriction of ajax. If you try to access a local file from a different domain, the Same-Origin Policy will deny access to the local file...
Same-Origin Policy is also happening in jQuery, but I studied a lot of things like why.

In short, I'm sorry if it doesn't work because I packed my knowledge on the spot.


2022-09-30 18:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.