Home > Back-end >  Unable to call ajax function but console.log and alert working of that JS file
Unable to call ajax function but console.log and alert working of that JS file

Time:12-29

hello everyone working on PHP MVC. Facing issue while submitting form using ajax, in Network area of Inspect elements not able to see that perticular ajax being called. If i try to run console.log or alert in that JS then it's working fine. here is my JS code. Thanks in advance.

$(document).ready(function(){
    $("button#applyLeave").click(function(){
        console.log('asgnduser');
        var leaveType = $("#leavetype").val();
        var leaveDate = $("#leavedate").val();
        var leaveTime = $("#leavetime").val();
        var leaveDateRange = $("#reservation").val();
        var leaveReason = $("#leavereason").val();
        console.log(leaveType   ' '  leaveDate  ' '  leaveTime   ' '  leaveDateRange   ' '  leaveReason);
        alert(APP_URL   '/apply-leave');

        var dataString = 'leaveType='  leaveType   '&leaveDate='  leaveDate   '&leaveTime='  leaveTime   '&leaveDateRange='  leaveDateRange   '&leaveReason='  leaveReason;
            // AJAX Code To Submit Form.
            $.ajax({
                type: "POST",
                url: APP_URL   '/apply-leave',
                data: dataString,
                cache: false,
                success: function(result){
                    // console.log('Success: ' result);
                    // alert('Data Saved Successfully');
                    if (result == 'success') {
                        alert("Leave applied!!!");  
                        window.location.href = APP_URL   '/leaves';
                    }   
                },
                error: function(result){
                    // console.log('Error: ' result);
                }
            });
        return false;
    });
});

CodePudding user response:

I just tried out your code in code sandbox environment and it's working properly. If you don't see any requests going out then most likely your button or it's ID is not existing in the HTML.

Here's my example code sandbox url: https://codesandbox.io/s/ecstatic-hugle-7rfnt?file=/src/index.js

You can do a compare with your code and see the difference.

CodePudding user response:

Did silly mistake over here. In inspect element Network tab recording of network log was turned off because of that wasn't able to see ajax there. Turning on I was able to see it.

  • Related