Accessing PageMethod from aspx page - c#

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

Related

jQuery Calls to .NET WebMethod Working Out of Order

I am using lot of .NET WebMethod calls in my web application using jQuery. My calls are not working in the expected order. The first time the page loads it works fine, and after reloading the page, the WebMethod calls are disordered. How do I make it always work one by one in the expected order?
I am using jQuery $.ajax():
$.ajax({
type: "POST",
url: "",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) { },
error: function (msg) { alert(msg.error); }
});
Add async: false option in ajax. By default Ajax make async call hence order is not guaranteed.
$.ajax({
type: "POST",
async : false, // Add this
url: "",
data: '',
contentType: "application/json;
charset=utf-8",
dataType: "json",
success: function (response) { },
error: function (msg) { alert(msg.error); }
});

Ajax URL - Incorrect .net #c

Been following some tutorials, as i'm learning C#, however was just attempting to make an ajax call to the server, i'm using my localhost.
As far as I can tell i'm doing its right, but it's obviously not.
Maybe it my folder structure or just the name of the file.
The Default.aspx file is at the project root
Here is my ajax call
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx",
dataType: "json",
data: "{'FirstName':'test','LastName':'test1','City':'test3','EmailID':'test4'}",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});
I know eventually it will have to call a method within the file, but its not even finding it at the moment.
Thanks in advance, James
You can use controller Home and create the one action.
Example:
public ActionResult FirstAjax()
{
return Json(true, JsonRequestBehavior.AllowGet);
}
After that your jquery ajax is:
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "Home/FirstAjax",
dataType: "json",
data: "",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});

Ajax Call in asp.net

I am making JQuery Ajax call in asp.net, I am returning String with my WebMethod, But on success of ajax call I am getting complete HTML of page in result.I also used type: "get" but no luck, below is my code for ajax call
$.ajax({
type: "POST",
url: "MyPage.aspx/GetData", //url to point your webmethod
success: function (Result) {
if (Result != "")
alert( Result);
},
error: function () { alert('error'); }
});
[System.Web.Services.WebMethod()]
public string GetData()
{
//getting value from Database and returning
}
I am calling this Ajax in MyPage.aspx
Try it like this. With the contentType: "application/json; charset=utf-8"
$.ajax({
type: "POST",
url: "MyPage.aspx/GetData", //url to point your webmethod
contentType: "application/json; charset=utf-8",
success: function (Result) {
if (Result != "")
alert( Result);
},
error: function () { alert('error'); }
});

How can Usercontrol access the Webmethod using Jquery and Json

$(document).ready(function () {
//hide the branch
var noteServiceUrl = "../../../AJAX/Common/BankingServiceAjax.svc/";
var proxyNotes = new serviceProxy(noteServiceUrl);
OnGetNotes();
function OnGetNotes() {
proxyNotes.invoke("SearchBranchesByBankID",
{ "request":
{
"BankID": 1
}
}, OnGetNotesComplete, OnError);
}
function OnGetNotesComplete(result) {
var obj = jQuery.parseJSON(result);
var notes = obj.Notes;
}
I cannot access the BankingServiceAjax.svc by usercontrol, But if do this in page, I can access the Service. The URL is correct I think I lack only one attribute that might access the BankingService in my Usercontrol.
it is going to be done in an AJAX call similar to this
$.ajax(
{
type: "GET",
url: 'http://fastmotorcycleservice.cloudapp.net/FastMotorcycleListService.svc/list/Bruno',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
Google is good too - How To Consume svc Webservice using jQuery

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

Categories