簡體   English   中英

jQuery AJAX:未捕獲的語法錯誤:意外的令牌

[英]jQuery AJAX: Uncaught SyntaxError: Unexpected token

當用戶使用搜索textbox時,我嘗試用新內容更新div內容:

$('#business_request_filter_search_textbox').on('input propertychange paste', function () {
    $.ajax({
        url: "/ajax/housekeeping/business/" + $("#search_filter_selection")[0].selectedIndex == 1 ? "get-requests-by-username" : "get-requests-by-business-name";
        type: "GET",
        cache: false,
        data: { search: $('input#business_request_filter_search_textbox').val() },
        beforeSend: function(xhr) {
            $('#request_area').html('<center>Please wait while we gather results...</center>');
        },
        success: function(data) {
            $('#request_area').html(data);
        },
    });
});

現在,我有一個下拉菜單,選擇他們要過濾搜索的內容,即用戶名或公司名稱。 這是引發錯誤的行。

url: "/ajax/housekeeping/business/" + $("#search_filter_selection")[0].selectedIndex == 1 ? "get-requests-by-username" : "get-requests-by-business-name";

難道我做錯了什么?

url行末尾應有一個逗號“,”:

$('#business_request_filter_search_textbox').on('input propertychange paste', function () {
    $.ajax({
        url: "/ajax/housekeeping/business/" + $("#search_filter_selection")[0].selectedIndex == 1 ? "get-requests-by-username" : "get-requests-by-business-name",
        type: "GET",
        cache: false,
        data: { search: $('input#business_request_filter_search_textbox').val() },
        beforeSend: function(xhr) {
            $('#request_area').html('<center>Please wait while we gather results...</center>');
        },
        success: function(data) {
            $('#request_area').html(data);
        },
    });
});

您沒有為ajax調用定義dataType。 添加dataType是您的ajax請求的預期格式,可以是文本,json等

試試這個代碼

$('#business_request_filter_search_textbox').on('input propertychange paste', function () {
    $.ajax({
        url: "/ajax/housekeeping/business/" + $("#search_filter_selection")[0].selectedIndex == 1 ? "get-requests-by-username" : "get-requests-by-business-name",
        type: "GET",
        cache: false,
        data: { search: $('input#business_request_filter_search_textbox').val() },
        beforeSend: function(xhr) {
            $('#request_area').html('<center>Please wait while we gather results...</center>');
        },
        success: function(data) {
            $('#request_area').html(data);
        },
    });
});

希望對您有所幫助:)享受:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM