在ajax中传递多个参数会给我一个错误

时间:2016-11-09 07:07:30

标签: javascript jquery ajax webmethod

我想将多个参数传递给我的ajax代码。哪个是3个参数。所以,我添加如下

$(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $("#txt712").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "FrmAgreementMaster.aspx/GetAutoCompleteData",
                    //data: "{'username':'" + extractLast(request.term) + "'}",
                    data: JSON.stringify("{'username':'" + extractLast(request.term) + "'}", "{'taluka':'" + document.getElementById('ddlTaluka').value + "'}", "{'village':'" + document.getElementById('ddlVillage').value + "'}"),
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            },
            focus: function () {
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                terms.pop();
                terms.push(ui.item.value);
                terms.push("");
                this.value = terms.join(", ");
                return false;
            }
        });
        $("#txt712").bind("keydown", function (event) {
            if (event.keyCode === $.ui.keyCode.TAB &&
                $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        })
        function split(val) {
            return val.split(/,\s*/);
        }
        function extractLast(term) {
            return split(term).pop();
        }
    }

参考Darin's代码的HERE

但是在调用函数时,它会带我到ERROR部分。如何传递多个参数以使用它。

更新

另外,我从here

获取了ajax引用

回复代码: -

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static List<string> GetAutoCompleteData(string username, string taluka, string village)
{
    List<string> result = new List<string>();
    using (OracleConnection ObjPriCon = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString()))
    {

        using (OracleCommand cmd = new OracleCommand("select distinct survey_area_7_12 FROM xxcus.xxacl_pn_farming_mst WHERE survey_area_7_12  " +
                                                     "LIKE '%' || :searchtext || '%' and taluka = '" + taluka + "' and village = '" + village + "'", ObjPriCon))
        {
            ObjPriCon.Open();
            cmd.Parameters.AddWithValue(":searchtext", username.ToLower());
            OracleDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    result.Add(dr["survey_area_7_12"].ToString());
                }
            }
            return result;
        }
    }
}

5 个答案:

答案 0 :(得分:5)

您可以传递多个参数:

$.ajax({
...
data: {
    var1: val1,
    var2: val2,
    var3: val3,
    var4: val4,
    // and many more
}
...
});

如果你想传递一个数组,你可以使用'JSON.stringify'。

答案 1 :(得分:1)

尝试传递这样的数据:

data: JSON.stringify([
    {username: extractLast(request.term)},
    {taluka: document.getElementById('ddlTaluka').value},
    {village: document.getElementById('ddlVillage').value}
]),

或者像这样:

data: {
    username: extractLast(request.term),
    taluka: document.getElementById('ddlTaluka').value,
    village: document.getElementById('ddlVillage').value
},

答案 2 :(得分:1)

您不需要多个对象,您需要在对象中使用多个属性

 data: JSON.stringify({username: extractLast(request.term) , taluka: $('#ddlTaluka').val(), village:$('#ddlVillage').val()}),

Ps:不要忘记在页面就绪时触发自动完成,确保在这些输入中有值

答案 3 :(得分:0)

最简单的方法是将包含数据键值对的对象分配给ajax调用的data属性。

$.ajax({
   type: 'POST',
   url: url,
   data: {
      'name': $('#name').val(),
      'email': $('#email').val(),
      // so on...
   }
})

然后您可以通过您在此处指定的名称在帖子请求中获取它们。

答案 4 :(得分:0)

请尝试这样做。

 $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "FrmAgreementMaster.aspx/GetAutoCompleteData",
                //data: "{'username':'" + extractLast(request.term) + "'}",

        data: JSON.stringify({
            username: extractLast(request.term),taluka:document.getElementById('ddlTaluka').value,village:document.getElementById('ddlVillage').value
        }),
                error: function (result) {
                    alert("Error");
                }
            });