Home > Enterprise >  PHP Javascript Ajax window.location Not Working
PHP Javascript Ajax window.location Not Working

Time:12-08

There is software that uses Ajax Get. The code below does not work. What can I do?

Javascript code:

    <script>
        function buy_product(cid) {
            $.ajax({
                url: '/buyProduct/'   cid,
                type: 'get',
                success: function(response) {
                    var result = $.parseJSON(response);
                    if (result["success"]) {
                        iziToast.success({
                            theme: 'dark',
                            message: result["message"],
                            timeout: 3000
                        }).then(function() {
                            window.location = "/my/products";
                        }, 3000);
                    } else {
                        alert("Error");
                    }
                },
                error: function(xhr, err) {
                    alert("Error");
                }
            });
        }
    </script>

Example response:

{"success":true,"message":"Success","status":"SUCCESS"}

Console error:

Uncaught TypeError: Cannot read properties of undefined (reading 'then')

If it output is success, "iziToast.success" is running. But window.location is not working.

CodePudding user response:

Use the onClosed option to specify a function that should be called when the user closes the dialog.

                        iziToast.success({
                            theme: 'dark',
                            message: result["message"],
                            timeout: 3000.
                            onClosed: function() {
                                window.location = "/my/products";
                            }
                        });

CodePudding user response:

                    var redirectFunc = function() {
                        window.location = "/my/products";
                    };
                    iziToast.success({
                        theme: 'dark',
                        message: result["message"],
                        timeout: 3000
                    });
                    setTimeout(redirectFunc, 3000);

This is how I solved the problem.

  • Related