Reputation: 4721
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;
}
}
}
Upvotes: 3
Views: 7446
Reputation: 26258
You can pass multiple params like:
$.ajax({
...
data: {
var1: val1,
var2: val2,
var3: val3,
var4: val4,
// and many more
}
...
});
In case you want to pass an array, than you can use 'JSON.stringify'.
Upvotes: 5
Reputation: 19
Please try to do this.
$.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");
}
});
Upvotes: 0
Reputation: 32354
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
Upvotes: 1
Reputation: 594
The simplest way is to assign an object containing the key value pair of your data to the data attribute of your ajax call.
$.ajax({
type: 'POST',
url: url,
data: {
'name': $('#name').val(),
'email': $('#email').val(),
// so on...
}
})
and then you can simply get them in your post request by the names you specify here.
Upvotes: 0
Reputation: 105499
Try to pass data like this:
data: JSON.stringify([
{username: extractLast(request.term)},
{taluka: document.getElementById('ddlTaluka').value},
{village: document.getElementById('ddlVillage').value}
]),
Or like this:
data: {
username: extractLast(request.term),
taluka: document.getElementById('ddlTaluka').value,
village: document.getElementById('ddlVillage').value
},
Upvotes: 1