2017-08-22 28 views
0

當我在我的應用程序的主頁上時,我的ajax調用工作得很好。Ajax調用只能在一個頁面上工作?

$('.upvote').click(function(event) { 
     $(this).siblings().removeClass('downVoted'); 
     event.preventDefault(); 

     var id = $(this).data("id"); 
     var token = $(this).data("token"); 
     var count = $(this).children().data('count'); 
     var altcount = $(this).siblings().children().data('count'); 
     $.ajax(
      { 
       url: 'upvote/' + id, 
       type: 'GET', 
       dataType: "JSON", 
       data: { 
        "id": id, 
        "method": 'GET', 
        "_token": token 
       }, 
       crossDomain: true, 
       done : 
        $(this).addClass('upVoted').children().html(count +1), 
       success: 
        $(this).siblings().children().html(altcount === 0 ? altcount : altcount -1) 
      }); 

}); 

但是當我切換到具有完全相同的點擊按鈕和功能作爲我的主要頁面不同的頁面,Ajax調用突然不再起作用。它只會在我的主頁上工作,我不知道爲什麼。

+0

是你的ajax腳本包含在不同的頁面? –

+0

在瀏覽器開發者工具控制檯中有用嗎?你有頁面中的元素,包括'upvote' –

+0

是的,我正在使用Laravel和ajax腳本是在我的佈局,它包含在我的應用程序的所有頁面。 – QuintonO14

回答

0

我剛剛找到答案。我不得不爲每個url添加另一個'/',現在它工作得很好。

相關問題