skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER
                        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.


  2. 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";
                                }
                            });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search