AutoComplete JQuery plugin and ASP.Net C# - c#

I'm using the JQuery autocomplete plug in, and am passing in an array of strings to autocomplete (see code below). The method I'm calling to get my data (GetCustomerNames) is just returning an array of strings, and that is working fine. I need to find some way to pass in a parameter to the GetCustomerNames Method so I can filter what is being returned. Can anyone help with this?
Here is the markup code in the Default.aspx page:
<head runat="server">
<title></title>
<script type="text/javascript" src="js/jquery-1.3.2.js" ></script>
<script type="text/javascript" src="js/jquery.autocomplete.js" ></script>
<script type="text/javascript">
//Working, but uses results output to an aspx page using StringBuilder, trying
//to find a way to get the data with json
//$(document).ready(function() {
// $("#example").autocomplete('AutoCompleteData.aspx');
//});
$(document).ready(function() {
$("#example").keyup(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetCustomerNames",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#example").autocomplete(msg.d);
},
error: function(msg) {
alert("error");
}
});
});
});
</script>
Customer Name:
And here is the code in the Default.aspx.cs code behind page implementing the GetCustomerNames method:
[WebMethod]
public static string[] GetCustomerNames()
{
string[] data = new string[] {"Andrew", "Ramona", "Russ", "Russell", "Raymond"};
return data;
}

You could use the data hash to pass parameters to the method:
$.ajax({
type: 'POST',
url: 'Default.aspx/GetCustomerNames',
data: '{ parameterName: "some test value" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(msg) {
$("#example").autocomplete(msg.d);
},
error: function(msg) {
alert("error");
}
});
And your web method becomes:
public static string[] GetCustomerNames(string parameterName)

Related

Accessing PageMethod from aspx page

I have a webforms application and need to make an jquery ajax call to a PageMethod (i.e. WebMethod) in code behind from my aspx page. So far it does not work for me. Is this possible?
Here is my code:
$(function()
{
setInterval(function(){
$.ajax({
type: "GET",
url: "/ClientBillingBillDetails.aspx/MyPageMethod",
data: {someData: '<%= TheData %>'},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
}
});
}, 10000);
});
[System.Web.Services.WebMethod]
public static string MyPageMethod(int someData)
{
return "";
}
Is something wrong with my URL or something else?
Thanks
Try This:
$(function () {
setInterval(function () {
$.ajax({
type: "POST",
url: "/ClientBillingBillDetails.aspx/MyPageMethod",
data: "{ 'someData': '<%= TheData %>' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
}
});
}, 10000);
});
Use type as post and make sure if you have ajax.jquery library reference added in solution.
Also i think you can remove '/' in specifying method..
Just use "ClientBillingBillDetails.aspx/MyPageMethod".
Else you can use simple pageMethods using scriptmanager

What is the url I will give to the ajax when I send json objcet to c# web server

i wrote a java script to send json object to web service in c#. My web service has a method to retrieve json object called getPerson(String text){}. I can`t understand what is the url that I will give to ajax.This is my java script.
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script>
<script type="text/javascript">
function BindJson() {
document.getElementById("demo").innerHTML=Date();
var $pString = {"firstName": "Rukshi"};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: $pString,
datatype: "json",
url: "http://localhost:54884/Service1.asmx/getPerson",
success: function (data, status) {
alert("ok");
},
error: function (msg) {
alert(msg.status);
}
});
}

Ajax call to c# function not working

I'm trying to use Ajax to call a C# function but the call is not working.The script shows the hello message but does not show the success/error message.What am i doing wrong
Java script
<script type="text/javascript">
$(document).ready(function () {
$('#btnsave1').click(function () {
alert("hello");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "LeaveSurrender.aspx/apply",
dataType: "json",
success: function () {
alert('Successfully Saved');
// window.location.href = "ClubCreation.aspx";
},
Error: function () {
alert('error');
}
});
});
});
C# Method
protected void apply()
{
MessageBox.Show("hi");
}
Try this:
[WebMethod]//write [WebMethod]
public static string apply()//method must be "pulic static" if it is in aspx page
{
return "Hi";
}
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "LeaveSurrender.aspx/apply",
dataType: "json",
data:'{}',
success: function (result) {
alert(result);
// window.location.href = "ClubCreation.aspx";
},
Error: function () {
alert('error');
}
});
Few things you need to fix here. First: there's no MessageBox in webforms. change the apply() method to return string:
protected string apply()
{
return "hi!";
}
Second: Use '#btnsave1' to '#<%= btnsave1.ClientID %>' to get server generated id for button and also catch the string returned by apply() method. Your script should look like:
<script type="text/javascript">
$(document).ready(function () {
$('#<%= btnsave1.ClientID %>').click(function () {
alert("hello");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "LeaveSurrender.aspx/apply",
dataType: "json",
success: function (data) {
alert(data);
// window.location.href = "ClubCreation.aspx";
},
Error: function () {
alert('error');
}
});
});
});
</script>
Third: Make sure you have referenced jquery in the head of your page:
<head runat="server">
<script src="Scripts/jquery-1.8.2.js"></script>

jquery ajax code is not passing values to webservice method

My ajax code is not passing values to my webservice method .. i think i am not doning it properly. please guide me.
this is my .aspx code:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: "{ 'ReachTime': '21-Nov-11', 'Destination': 'location' }",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
and this is my webservice method
public static void InsertRediretTime(string ReachTime, string Destination)
{
//operational code
}
Thanks in advance
Take out the static keyword from your method.
public void InsertRediretTime(string ReachTime, string Destination)
{
//operational code
}
Try this:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: "ReachTime=21-Nov-11&Destination=location",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
Although your service is expecting id and order, but you're passing ReachTime and Destination - is this correct?
What is the error that you are getting? See tool such as Fiddler (or Firebug on Firefox) to inspect the request/response - see the response for your ajax request - that will help you to troubleshoot the issue.
OTH, you need a ScriptService attribute applied to your web service class. If you are using .NET 2.0/3.5 then you also need configuration entries to register ScriptHandlerFactory handler that is responsible for JSON support in asmx services. See this article for more info regarding configuration: http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/
Thanks everyone for your help ... a combination of your help worked for me .. here is the solution:
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: '{ ReachTime: "21-Nov-11", Destination: "location" }',
contentType: 'application/json; charset=utf-8',
dataType: JSON,
success: function (data, status) {
alert(data.d);
}
});
});
and,
public void InsertRediretTime(string ReachTime, string Destination)
{
blah blah
}
Thanks again :)
Try this,
in aspx page
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/InsertRediretTime",
data: '{ReachTime:21-Nov-11,Destination:location}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, status) {
alert(data.d);
}
});
});
In webservice
public string InsertRediretTime(string ReachTime, string Destination)
{
//operational code
return stringData;
}

How do I return the current domain of my project in javascript and ASP.NET?

I have some javascript that requires a specific URL to call an ASP.NET web service.
When I run the application locally, the url is something like: http://localhost:123456/ProjectName/Default.aspx
But when I upload the application, the domain will change to:
http://myDomain.com/Default.aspx
What's the best way to capture the current URL path and pass it in as a variable to my javascript? Here is my javascript:
<script type="text/javascript">
$(document).ready(function () {
$("#autoComplete").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:123456/ProjectName/Default.asmx/SkillsList", //CHANGE THE URL!!
//url: "http://myDomain.com/Default.asmx", //The other URL...
data: "{'like':'" + request.term + "'}",
datatype: "json",
async: true,
success: function(data) {
response(data.d);
},
error: function(result) {
alert("error");
}
});
},
minLength: 2
});
});
</script>
I'm using ASP.NET if that helps at all.
Thanks!
post to relative path
url: "/ProjectName/Default.asmx/SkillsList"
Looks like you're writing out to the aspx page. Given that, you should be able to just have ASP.Net tell you via something like
url: "<%= Request.ApplicationPath%>/Default.asmx/SkillsList"

Categories