【问题标题】:Route not accessible from ajax function in a seperate js file in online server无法从在线服务器的单独 js 文件中的 ajax 函数访问路由
【发布时间】:2021-11-16 13:17:43
【问题描述】:

我有一个 laravel 项目。每个视图页面都位于单独的 js 文件中。问题是我无法从 ajax post 访问路由功能或在在线服务器(数字海洋)中接听电话。 错误是:

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

相同的应用程序在我的本地系统中运行良好。 当我在设计(blade.php)页面中添加 ajax 函数时,它在服务器中工作正常。在设计中改变我所有的功能并不是那么令人愉快。 直到昨天,服务器和应用程序都运行良好。我对设计进行了一些更改,并将其从 bitbucket 拉到了今天的服务器上。之后发生了这个错误。 从 bitbucket 中提取后,我必须在服务器上做些什么吗?我只提取了对设计所做的更改。

我的路由功能:

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

AJAX 函数:

 $.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);
    }
});

在线服务器上运行ajax的错误函数。

【问题讨论】:

  • 你应该在ajax函数中使用路由方法,比如url: '{{route('invoice_vno_bind')}}'
  • 我在我的本地系统中使用相同的代码,它工作正常..@AmitSenjaliya
  • 本地和服务器设置不同,因此请确保正确的路由 URL 在 ajax 中传递。
  • 直到昨天都运行良好。 @AmitSenjaliya
  • 请在服务器 url: '{{route('invoice_vno_bind')}}' 上尝试使用 route 方法,因为服务器可能处于仅考虑完全匹配 URL 的严格模式

标签: javascript php ajax laravel digital-ocean


【解决方案1】:

首先,blade.php中添加隐藏参数。

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

然后在js文件中获取id="ajax_url"值。

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

【讨论】:

  • 对不起。相同的代码在我的本地系统中运行良好,直到昨天也很好。使用 git pull 进行一些设计更改后,这个问题就开始了。
  • @FaheemMCFC 您是否手动检查 URL 是否正常工作?喜欢在浏览器中点击网址http://yourDomainName/invoice_vno_bind
  • 是的,我检查过了,我成功地得到了数据@AmitSenjaliya
【解决方案2】:

最后我得到了错误。这是因为一些程序行 /etc/nginx/sites-enabled/{your file name}

我把下面的代码注释掉了:

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

评论后效果很好。我不知道这是否会影响任何其他领域。到目前为止,它运行良好。

谢谢大家的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2011-12-14
    • 2021-04-09
    • 2018-04-12
    • 2017-01-15
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多