Search code examples
javascriptphpajaxlocation

PHP Javascript Ajax window.location Not Working


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.


Solution

  • 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";
                                }
                            });