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

It doesn't work with JavaScript's "fetch" api #17

Open
SalehBagheri opened this issue May 30, 2023 · 1 comment
Open

It doesn't work with JavaScript's "fetch" api #17

SalehBagheri opened this issue May 30, 2023 · 1 comment

Comments

@SalehBagheri
Copy link

I am using asynchronous 'fetch' to send requests. The endpoint actions call the "_notyfService.Success" method, but the notyf doesn't work and it is not displayed on the screen.

JS code:
let response = await fetch('/controller/action');

and in Controller's action, I have:
_notyfService.Success("Text"); return new EmptyResult();

Any suggestion?

@SalehBagheri
Copy link
Author

After hours of exploring, (According to this line below):

public static bool IsNotyfAjaxRequest(this HttpRequest request)

I finally found out that you need to add this line to each request's header in order to be identified as a Notyf request:

'X-Requested-With': 'Notyf'

and then, copy and modify just the first two lines of the method below:

to this:

function getResponseHeadersFETCH(response) {
    var notificationHeader = response.headers.get("X-Notyf-Notifications"); // <-- Just this line has been modified.
    if (notificationHeader !== null) {
        notificationHeader = notificationHeader.split("\n");
        notificationHeader.forEach(function (header) {
            header = header.split(": ");
            var key = header.shift();
            if (key.length == 0) return;
            var decodedData = decodeURIComponent(key);
            var notifications = JSON.parse(decodedData);
            notifications.forEach(function (notification) {
                if (notification.type == 0) {
                    toastNotifySuccess(notification.message.replace(/\+/g, ' '), notification.duration);
                }
                else if (notification.type == 1) {
                    toastNotifyError(notification.message.replace(/\+/g, ' '), notification.duration);
                }
                else if (notification.type == 2) {
                    toastNotifyWarning(notification.message.replace(/\+/g, ' '), notification.duration);
                }
                else if (notification.type == 3) {
                    toastNotifyInformation(notification.message.replace(/\+/g, ' '), notification.duration);
                }
                else if (notification.type == 4) {
                    toastNotifyCustom(
                        notification.message.replace(/\+/g, ' '),
                        notification.duration,
                        notification.backgroundColor,
                        notification.icon);
                }
            })
        });
    }
}

then, call this function after each request manually.

It's not a good idea, but can solve the problem temporarily.

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

No branches or pull requests

1 participant