Send request to server side on the text using asp.net - javascript

I want to send request on server side onFocus event but my method is not calling, really confused, tried jquery ajax for that but all failed is there any other possible way to send request on server side using asp.net textbox onFocus event?
asp.net website project is created for work.
Ajax method code
$.ajax({
type: "POST",
url: "About.aspx/GetRes",
data: "{}",
contentType: "application/text; charset=utf-8",
dataType: "text",
success: function (msg) {
alert(msg);
}
});
Method which is calling
Public Function GetRes() As String
Return "I am calling GetRes method"
End Function

If you are able to access the supplied url via browser properly then please try this..
$.ajax({
type: "POST",
url: "About.aspx/GetRes",
dataType: "text",
success: function (msg) {
alert(msg);
}
});
Also, i'm considering that your event biding is wrapped under the document.ready callback
and make the server side method like this
<WebMethod>
Public Shared Function GetRes() As String
Return "I am calling GetRes method"
End Function

You can only call PageMethods like that.
Such methods should be static(Shared) and have the WebMethod attribute.
<WebMethod()>
Public Shared Function GetRes() As String
Return "I am calling GetRes method"
End Function

Related

Asp.net Server code executes two times and returns failed callback method

Im calling server code (c#) by jQuery ajax method or ajax pagemethods. While calling, server code executes two times and returns failed callback method.
This code is not working in recently hosted new server. In existing servers, this code working without any problem and returning success callback method.
JS Code:
var myVal = 'Test val';
var input = {
'val1': myVal
};
$.ajax({
async: true,
type: 'POST',
url: 'MyPage.aspx/MyFunction',
data: JSON.stringify(input),
dataType: 'json',
contentType: 'Application/json',
success: QCErrorResult,
error: QCErrorFailedMethod
});
OR
JS Code:
PageMethods.MyFunction(myVal, QCErrorResult, QCErrorFailedMethod);
C# Code:
[WebMethod(EnableSession = false)]
public static string MyFunction(string val1)
{
// My code here
}
Same issue while calling my api by jquery ajax.
In App_Start/RouteConfig comment the line settings.AutoRedirectMode =RedirectMode.Permanent if its there. For multiple times calling, check the post backs of the page.

Problem while using AJAX requesting webmethod info from VB file

I've been trying to call a VB.NET function from JavaScript for the website I've been working on, and found out about AJAX and started trying to use it.
(For reference, my target framework is 4.6.1.)
The following is a snippet of my VB.NET code, where PopulateDetailSection() is something which returns a string which is supposed to be the text from the div, but my breakpoint never hits this function.
System.Web.Services.WebMethod(EnableSession:=True, BufferResponse:=False)
Protected Shared Function PopulateDetail() As HtmlGenericControl
Return PopulateDetailSection()
End Function
and as for the AJAX call:
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "GET",
//contentType: "application/json: charset=utf-8",
dataType:"html",
success: function (data) {
alert(data);
}
});
I've tried alerting several things, but it keeps returning undefined unless I alert data which appears to return the aspx file starting with the header.
I usually don't ask questions here, but I'm truly stumped on this.
There some issues with your JavaScript. As described here: https://stackoverflow.com/a/5332290/428010 you need to post on the web page/method.
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "POST",
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
On the VB side the method need to be public not protected.

Ajax do not pass data to method using GET

I have a method which Is accesed using Get
[HttpGet]
[AdminAuthorization]
public ActionResult MakeReservation(ReservationModel m)
{
return PartialView(m);
}
here Ajax Code:
$.ajax({
url: "/DeviceUsage/MakeReservation",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({ data: Ids }),
error: function (data) {
alert("Dodanie nie powiodło się Jeden lub wiecej numerów seryjnych nie są unikalne " + data);
},
success: function (data) {
$('#ProperModal.modal-body').html(data);
$("#Modal").modal('show');
//if (data === "sukces") {
}
});
If I change method description and ajax type to POST function works. How should I modify this code to make it work wiht GET calls?
You need to use JsonRequestBehavior.AllowGet in your controller. For more information you could read this answer on SO
And I think it is good practise to return Json (not PartialView) in your action (for ajax). If you want to return PartialView, you could use this technique
You don't need to explictly tell the HttpGet, By Default, it takes it as HttpGet, but if you put HttpPost attribute, then it does not work on Get Requests.
Same is the case for Jquery ajax, if you don't tell it, its get or post request, it by default makes a get request to server
Remove contentType and dataType: 'json' (this indicates you are returning json, but your code returns a partial view). And Remove JSON.stringify as jQuery accept your JS object directly. Haven't tested it, but it should work.

JQuery server side method call internal

i often call my aspx server side method with the help of jquery....like
$.ajax({
type: "POST",
url: "login.aspx/Authenticate",
data: JSON.stringify(DTO),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
sHtml = data.d;
if (sHtml != "") {
alert(sHtml);
location.href = sHtml;
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
but the funny things is that i dont know how jquery call my static method from outside.
i saw that when i call my server side method then page_load does not fire but in case of updatepanel partial postback page_load execute first.
so i want to know the internal logic of jquery that how it can call server side method directly........looking for good explanation. thanks
so i want to know the internal logic of jquery that how it can call server side method directly
It can't.
jQuery can cause the browser to make an HTTP request to a URI.
The server can run code in response to a URI being requested in order to decide what content and headers to return.
What happens in jquery case is that the method is marked as webmethod which means that it works as an endpoint for an httprequest as if it is a webservice and when you do that, then the jquery make a httprequest to this method as if he is calling a webservice.
this URL will give you a deep view on what happen when you are making any ajax call http://msdn.microsoft.com/en-us/magazine/cc163499.aspx
your server side methods are marked with the [WebMethod] attribute, right? this attribute exposes the method as an xml web service.
http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.71).aspx
since this autogenerated "webservice" has nothing to do with the page, page_load is not called.

is there a way to set an asp.net session variable from a javascript/jquery link?

Basically i am trying to set a session when a user clicks a specific button is this possible?
So i need to set this session
Session("TenHolStDateNewCheck") = "%"
When this link is clicked
blahblah
thanks
Jamie
You need a server side code to set session, use $.ajax() function
Using jQuery with ASP.NET
You can use something like this:
Server side (C#)
public partial class _Default : Page
{
[WebMethod]
public static void SetSession()
{
...
}
}
Client side (aspx)
$.ajax({
type: "POST",
url: "Default.aspx/SetSession",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function() {
alert('SetSession executed.');
}
});
You could do it with a ajax call to a page that sets the session variable of choice to whaterver you send along with the ajax call
See: jQquery Ajax
You can implement JSON-RPC set_session_var method and then in JQuery with $.ajax send json-rpc request to set_session_var method.

Categories