Home > database >  Route not accessible from ajax function in a seperate js file in online server
Route not accessible from ajax function in a seperate js file in online server

Time:09-23

I have a laravel project. Each view page has it's on seperate js file. Problem is I am not able to access route functions from ajax post or get call in online server(digital ocean). Error is:

Failed to load resource: the server responded with a status of 404 (Not Found)

The same application is working fine in my local system. When I add the ajax function inside the design (blade.php) page it's working fine in server. It's not so pleasing to change all my functions inside design. The server and application was working fine till yesterday. I made some changes in design and pulled it from bitbucket to server today. After that this error occurred. Is there anything I have to do in server after pulling from bitbucket. I only pulled the changes made in design.

My route function:

Route::prefix('invoice_vno_bind')->group(function () {
        Route::get('/', 'InvoiceController@getnextvno')->name('invoice_vno_bind');
    });

AJAX function:

 $.ajax({
    type: "GET",
    url: "invoice_vno_bind",               
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
        $('#txtVNo').val(data);
    },
    failure: function (response) {
        alert(response.d);
    },
    error: function (response) {
        alert(response.responseText);
    }
});

Error function of ajax is executed when running in online server.

CodePudding user response:

First, blade.php in add hidden parameter.

<input type="hidden" name="ajax_url" id="ajax_url" value="{{ route('invoice_vno_bind') }}">

And then get id="ajax_url" value in js file.

var url = $('#ajax_url').val();
$.ajax({
    type: "GET",
    url: url,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
        $('#txtVNo').val(data);
    },
    failure: function (response) {
        alert(response.d);
    },
    error: function (response) {
        alert(response.responseText);
    }
});

CodePudding user response:

Finally I got the error. It was because of some program lines in /etc/nginx/sites-enabled/{your file name}

I commented out the codes below :

#    location ~ /.(?!well-known).* {
#       allow all;
#    }
}

After commenting it worked fine. I don't know if this will affect any other fields. Till now it's perfectly working.

Thank you all for your suggestions.

  • Related