nad nad - 1 month ago 16
Ajax Question

Passing multiple parameters in ajax is giving me error

I want to pass multiple parameters to my

ajax
code. Which is
3
parameters. So, I added like below

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


By taking reference from HERE of
Darin's
code.

But it is taking me to
ERROR
part while calling the function. How do I pass multiple parameters to use it.

update

Also, I took the ajax reference from here

Response code:-

[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;
}
}
}

Answer

You don't need multiple object, you need multiple properties in a object

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

Ps: Don't forget that you trigger the autocomplete at page ready, make sure you have values in those inputs