简体   繁体   English

jQuery AJAX:未捕获的语法错误:意外的令牌

[英]jQuery AJAX: Uncaught SyntaxError: Unexpected token

I am trying to update my div content with new content when the user uses the search textbox : 当用户使用搜索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);
        },
    });
});

Now I have a dropdown selecting what they want to filter the search by, the username or the business name. 现在,我有一个下拉菜单,选择他们要过滤搜索的内容,即用户名或公司名称。 This is the line that is throwing the error. 这是引发错误的行。

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

Am I doing something wrong? 难道我做错了什么?

You should have a comma ',' at the end of the url line: 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);
        },
    });
});

You don't have dataType defined for the ajax call. 您没有为ajax调用定义dataType。 Add dataType which is the expected format of your ajax request which can be text, json etc 添加dataType是您的ajax请求的预期格式,可以是文本,json等

Try This Code 试试这个代码

$('#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);
        },
    });
});

Hope This help You :) Enjoy :) 希望对您有所帮助:)享受:)

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

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