Help with error

I’m using this video web script where videos can be purchased to view.
There are a couple of error messages in the code. I added ‘One’ and ‘Two’ at the end of each error message, just to see which error message was displaying upon Proceeding with a purchase, and upon testing it is always “‘Something went wrong. Please try again later!One’,” hoping to narrow down what the issue is, but it’s beyond my troubleshooting knowledge.

Any help or guidance on what to check or look at, or if you need me to provide more code to view, to resolve this issue in order to proceed with purchase test, will be appreciated:

function PT_MultipleBuyVideo() {
    var checked = getSelectedVideos();
    if (!checked) { return false; }

    swal({
        title: "",
        type: "info",
        html:"Simply proceed to purchase " + countSelectedVideos() + " videos(s) at a total cost of " + countTotalCredits() +" credits",
        showCancelButton: true,
        cancelButtonText: "Close",
        customClass: 'sweetalert-lg',
        confirmButtonText:'Proceed'
    }).then(function(){

        $.ajax({
            url: PT_Ajax_Requests_File() + 'aj/buy-video',
            type: 'POST',
            dataType: 'json',
            data: {id:checked},
        }).done(function(data){
            if (data.status == 200) {
                for (var i = 0; i < checked.length; i++) {
                    var button = $("button[data-action='multiple_select_button'][data-id='" + checked[i] + "']")
                    buttonMultipleSelectingStyle(button, 'purchased');
                }

                swal({
                    title: "Success",
                    type: "success",
                    html:"",
                    showCancelButton: true,
                    cancelButtonText: "Close",
                    customClass: 'sweetalert-lg',
                    confirmButtonText:'Go To Video(s)'
                }).then(function(){
                    window.location.href='/paid-list';
                });

            } else {
                if (data.error_num == 1) {
                    swal(
                        'Error!',
                        'Not enough money',
                        'error'
                    );
                } else {
                    swal(
                        'Error!',
                        'Something went wrong. Please try again later!One',
                        'error'
                    );
                }
            }
        }).fail(function() {
            swal(
                'Error!',
                'Something went wrong. Please try again later!Two',
                'error'
            );
        })
    });
}

function buttonMultipleBuy(command) {
    var button = $("button[data-action='multuple-buy-video']");
    if (command == 'hide') {
        button.hide();
    } else if (command == 'show') {
        button.show();
    }
}

I recommend that you remove the else clauses from the code, as having them there does help to confuse things.

You can easily achieve that by returning the swal function.

            if (data.status == 200) {
                // commented out for brevity
                return swal({
                    // commented out for brevity
                }).then(function(){
                    window.location.href='/paid-list';
                });
            }
            if (data.error_num == 1) {
                return swal(
                    'Error!',
                    'Not enough money',
                    'error'
                );
            }
            return swal(
                'Error!',
                'Something went wrong. Please try again later!One',
                'error'
            );

Before that last return swal is a good place to check what you get for data.status and data.error_num

Thanks for your reply.

I have tried what I believe you suggested like this:

 if (data.status == 200) {
                for (var i = 0; i < checked.length; i++) {
                    var button = $("button[data-action='multiple_select_button'][data-id='" + checked[i] + "']")
                    buttonMultipleSelectingStyle(button, 'purchased');
                }

                swal({
                    title: "Success",
                    type: "success",
                    html:"",
                    showCancelButton: true,
                    cancelButtonText: "Close",
                    customClass: 'sweetalert-lg',
                    confirmButtonText:'Go To Video(s)'
                }).then(function(){
                    window.location.href='/paid-list';
                });

            //} else {
                if (data.error_num == 1) {
                    return swal(
                        'Error!',
                        'Not enough money',
                        'error'
                    );
                  }
           }
        }).fail(function() {
            return swal(
                'Error!',
                'Something went wrong. Please try again later!TWO',
                'error'
            );
        })
    });
}

I don’t know what adding “return” does, and now I see this error displayed:

" Error!
Something went wrong. Please try again later!TWO"

Also, I don’t know how to “check what you get for data.status and data.error_num”.
any additional assistance is appreciated.

In your updated code, you haven’t returned from the first swal.

You can use console.log check the values, for example: console.log(data.status);

Thanks again for your reply.

I have tried this:

        $.ajax({
            url: PT_Ajax_Requests_File() + 'aj/buy-video',
            type: 'POST',
            dataType: 'json',
            data: {id:checked},
        }).done(function(data){
            if (data.status == 200) {
                for (var i = 0; i < checked.length; i++) {
                    var button = $("button[data-action='multiple_select_button'][data-id='" + checked[i] + "']")
                    buttonMultipleSelectingStyle(button, 'purchased');
                }

               return swal({
                    title: "Success",
                    type: "success",
                    html:"",
                    showCancelButton: true,
                    cancelButtonText: "Close",
                    customClass: 'sweetalert-lg',
                    confirmButtonText:'Go To Video(s)'
                }).then(function(){
                    window.location.href='/paid-list';
                });

           // } else {
                if (data.error_num == 1) {
                   return swal(
                        'Error!',
                        'Not enough money',
                        'error'
                    );
               // } else {
                   return swal(
                        'Error!',
                        'Something went wrong. Please Log-In!',
                        'error'
                    );
                }
            }
        }).fail(function() {
            return swal(
                'Error!',
                'Something went wrong. Please try again later message One!',
                'error'
            );
            console.log(data.status);
        })
    });
}

and I see this error message: “Something went wrong. Please try again later message One!”

and this in the Console (image attached). Any additional guidance is appreciated

console

Move the console.log line before the return swal to see more useful information.

The fail function has three parameters

}).fail(function(jqXHR, textStatus, errorThrown) {

You should be able to examine things like textStatus and errorThrown to learn more about what occurred.

Thanks again for your message.

I have tried this:

        }).fail(function() {
			console.log(data.status);
			//console.log();
            return swal(
                'Error!',
                'Something went wrong. Please try again later message One!',
                'error'
            );

        })
    });
}

which shows on the dev tools > console that the line console.log(data.status); is undefined, and search?keyword=c:445

when I change it to console.log(); it just shows search?keyword=c:445

any additional guidance to resolve this error is appreciated

I recommend updating the fail function parameters so that you can gain further information about what’s going on.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.