当我在应用程序的主页上时,我的 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 调用突然不再起作用。它只能在我的主页上使用,我不明白为什么。
其实我现在就找到了答案。我必须在每个网址中添加另一个“/”,现在它工作得很好。
$(document).ready(function () {
$("#dashBoardSearchBar").on("keyup", function () {
var query = $(this).val();
var url = window.location.origin + "/search"; // Construct the URL
$.ajax({
url: url,
type: "GET",
data: { search: query },
success: function (data) {
$("#searchResultsContainer").html(data);
},
});
});
});
使用这个 var url = window.location.origin + "/search";对于网址