Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error in InterceptAjaxRequestJS 'Failed to set responseType property' #1904

Merged
merged 2 commits into from
Dec 6, 2023

Conversation

EArminjon
Copy link
Contributor

@EArminjon EArminjon commented Dec 6, 2023

Description :

InAppWebView fail to intercept sync ajax query.

From android chrome console :

Capture d’écran 2023-12-06 à 14 50 19
(Js inspector)
Capture d’écran 2023-12-06 à 14 50 14

To reproduce

initialOptions:

  • useShouldInterceptAjaxRequest: true
  • javaScriptEnabled: true

initialData:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AJAX Example</title>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>

<button id="fetchDataBtn">Obtenir des données via AJAX</button>

<script>
    $('#fetchDataBtn').on('click', function () {
        $.ajax({
            url: 'https://jsonplaceholder.typicode.com/todos/1', // free open api
            async: false,
            method: 'GET',
            success: function (response) {
                console.log('Response AJAX :', response);
            },
            error: function (error) {
                console.error('Erreur AJAX :', error);
            }
        });
    });
</script>

</body>
</html>
Future<AjaxRequestAction> _onAjaxProgress(
      InAppWebViewController controller,
      AjaxRequest? ajaxRequest,
      ) async {
    print('test _onAjaxProgress $ajaxRequest');
    return AjaxRequestAction.PROCEED;
  }

  Future<AjaxRequestAction?> _onAjaxReadyStateChange(
    InAppWebViewController controller,
    AjaxRequest ajaxRequest,
  ) async {
    print('test _onAjaxReadyStateChange $ajaxRequest');
    return AjaxRequestAction.PROCEED;
  }

@pichillilorenzo
Copy link
Owner

Thanks, I will update the same code for iOS and macOS.
By the way, the fix should be && self.isAsync and not && result.isAsync, otherwise if you set isAsync to false on Flutter side, you will have the same exception.

@EArminjon
Copy link
Contributor Author

Okay, I changed and I didn't see the issue.
(little question : async and sync ajax are well handled by this package right ?)

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Dec 6, 2023

Unfortunately, due to Flutter platform channels async nature, if you use useShouldInterceptAjaxRequest and the ajax request uses isAsync with false, the XMLHttpRequest.send() will not wait for the response.
Probably, I should update the docs about it.
The shouldInterceptAjaxRequest and the other ajax events are custom, there is no official WebView native API.

@EArminjon
Copy link
Contributor Author

Oh ! thanks for your answer !

@EArminjon
Copy link
Contributor Author

@pichillilorenzo Is it possible to avoid (maybe with a parameter) synchronous ajax requests to be intercepted ?

@pichillilorenzo pichillilorenzo changed the title Update InterceptAjaxRequestJS.java Fixed error in InterceptAjaxRequestJS 'Failed to set responseType property' Dec 6, 2023
@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Dec 6, 2023

@EArminjon can you open a new "Feature request" in the issue page about that? Thanks

@pichillilorenzo pichillilorenzo merged commit 5fb5e8a into pichillilorenzo:master Dec 6, 2023
@pichillilorenzo
Copy link
Owner

In the meantime, I have released new versions for android, ios and macos platform implementation packages.
You can get the fix on latest plugin version 6 using flutter upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants