简体   繁体   English

如何修复使用 ajax 和 laravel 访问路由时未找到的 404

[英]How to fix 404 not found when access the route using ajax and laravel

I'm currently new developer in laravel, I have problem accessing the route, if I tried to click the edit button on my response it gives me error.我目前是 laravel 的新开发人员,我在访问路由时遇到问题,如果我尝试单击响应中的编辑按钮,则会出现错误。 this error shows on my production, but on my localhost, this error is not shown.此错误显示在我的生产中,但在我的本地主机上,未显示此错误。 my question is why this error is shown?我的问题是为什么会显示此错误?

"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException", "异常": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",

Ajax:阿贾克斯:

$('.news_and_events_edit_btn').on('click',function(e){

    e.preventDefault();

    var id = $(this).attr("data-content-id-edit");

    $('.news_and_events_hidden_update_id').val(id);

    $.ajax({
        url:'/get_news_and_event_data',
        type:'get',
        data:{content_id: id},
        dataType:'JSON',
        success:function(res) {
            $('#update_news_and_events_title').val(res[0].content_title);
             update_appEditor_news_and_event.data.set(res[0].content_desc);

        },
        error:function(err) {
            console.log(err);

        }
    });
});

Route:路线:

Route::get('/get_news_and_event_data','CMSController@get_news_and_event_data')->name('get_news_and_event_data');

Error:错误:

错误

change your route and ajax url with your content_id parameter and than try使用 content_id 参数更改routeajax url ,然后尝试

Route路线

Route::get('/get_news_and_event_data/{content_id}','CMSController@get_news_and_event_data')->name('get_news_and_event_data');

AJAX Call AJAX 调用

     $.ajax({
        url:'/get_news_and_event_data/{content_id}',
        type:'get',
        data:{content_id: id},
        dataType:'JSON',
        success:function(res) {
            $('#update_news_and_events_title').val(res[0].content_title);
             update_appEditor_news_and_event.data.set(res[0].content_desc);

        },
        error:function(err) {
            console.log(err);

        }
    });

You should use the route in ajax request .您应该在 ajax request 中使用route It will redirect you to route .它会将您重定向到route

$.ajax({
        url:"{{ route('get_news_and_event_data') }}",
        type:'get',
        data:{content_id: id},
        dataType:'JSON',
        success:function(res) {
      }

For example, I have a code ,例如,我有一个代码,

jQuery.ajax({
   url: "{{ route('SomeUrlHere') }}",
   method: 'get',
   data: {
       id : id
  }
success: function(data){
    if(data) {
         console.log(data);
    }

Change the url to something like this.将 url 更改为这样的内容。

var id = content_id;

$.ajax({
        url: '{{ URL::route('get_news_and_event_data') }}/'+id,
        type:'get',
        data:{content_id: id},
        dataType:'JSON',
        success:function(res) {
            $('#update_news_and_events_title').val(res[0].content_title);
             update_appEditor_news_and_event.data.set(res[0].content_desc);

        },
        error:function(err) {
            console.log(err);

        }
    });

To solved my problem, I found that there is a sub folder, so from url:'/get_news_and_event_data', to url:'/..get_news_and_event_data',为了解决我的问题,我发现有一个子文件夹,所以从 url:'/get_news_and_event_data',到 url:'/..get_news_and_event_data',

thanks to all谢谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM