Laravel Ajax URL在本地实时和正常工作中返回404错误

时间:2019-05-02 09:00:12

标签: ajax laravel

哪里出错了?这是我的Ajax代码

    $('.statusUpdate').click(function(){
    var update_id = $(this).attr('rel');
    var toggle_class = $(this).find('i').attr('class');
    var status = (toggle_class == 'fa fa-toggle-off')? 'Y':'N';        
    $.ajax({
        type: "POST",
        url: "/menugroup/update",
        data: {'id':update_id,'status':status},
    })
    .done(function(response){
            if(response == 'success'){
                update_class = (status=='Y')? 'fa fa-toggle-on':'fa fa-toggle-off';
                $('#status_'+update_id).find('i').attr('class',update_class);
            }
    });
});

在我使用过的路由器中

Route::post('/menugroup/update','MenugroupController@update');

在本地,点击生成效果很好

APP_URL=http://localhost

http://127.0.0.1:8000/menugroup/update

但是在实际情况下,项目放置在dinesapp文件夹中

APP_URL=http://xxx.xxx.xxx.xxx/dinesapp

并且在ajax请求中,实时网络标签上显示http://xxx.xxx.xxx.xxx/menugroup/update 而不是http://xxx.xxx.xxx.xxx/dinesapp/menugroup/update,这反过来在Laravel中返回404错误。在Laravel中是否需要配置任何基本路径?

2 个答案:

答案 0 :(得分:1)

您可以做一件事来解决此问题:

在标题部分定义基本网址:

<script>
   var base_url = '{{ URL::to("/") }}';
</script>

然后像这样调用ajax:

$('.statusUpdate').click(function(){
    var update_id = $(this).attr('rel');
    var toggle_class = $(this).find('i').attr('class');
    var status = (toggle_class == 'fa fa-toggle-off')? 'Y':'N';        
    $.ajax({
        type: "POST",
        url: base_url +"menugroup/update",
        data: {'id':update_id,'status':status},
    })
    .done(function(response){
            if(response == 'success'){
                update_class = (status=='Y')? 'fa fa-toggle-on':'fa fa-toggle-off';
                $('#status_'+update_id).find('i').attr('class',update_class);
            }
    });
});

答案 1 :(得分:0)

我明白了,问题是前斜线

class Catalogue extends React.Component{
    constructor(props) {
    super(props);
       this.state = {              
          dropdownOpen: false,              
          cards: [{id: 1, name: "Jack", visible: false}, {id: 2, name: "Queen", visible: false}]
       }
    }

    toggleCard = (cardId) => {
       const cards = this.state.cards
       //flip card, update state
       const updatedCards = cards.map((card, index, array) => {
           if(card.id == cardId){
               return {
                  ...card,
                  visible: !array[index].visible 
               }
           } else {
               return {
                  ...card 
               }
           }
       })
       this.setState({
           cards: updatedCards
       })
    }
    render(){
       const cards = this.state.cards
       return(
           cards.map((card) => {
               return <div onClick={() => this.toggleCard(card.id)}>{card.name}</div> 
           })

       )
    }
}

以下代码有效。基本网址更改时如何发生  当我们在前面添加斜杠

 $.ajax({
    type: "POST",
    url: "/menugroup/update",
    data: {'id':update_id,'status':status},
})