Can Page Methods be used in .net 2.0 with Ajax extensions? - asp.net

I need to call a server side method from JavaScript function in ASP.Net 2.0 framework. How can I do this?

Try This
function focuslost() {
mainForm.StartUpdating();
var pagePath = window.location.pathname;
$.ajax({
type: "POST",
url: pagePath + "/TextChanged",
data: ("{ 'pNTID':'" + $("#<%= txtNTID.txtClientId%>").val()) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function(XMLHttpRequest, textStatus, errorThrown) {
mainForm.EndUpdating()
},
success:
function(result) {
if (result.d.length > 0) {
}
mainForm.EndUpdating()
}
});
}
[WebMethod]
public static string TextChanged(string pNTID)
{
retrun "";
}

Use WebMethod or AjaxMethod. Check Below
http://www.xdevsoftware.com/blog/post/Call-WebMethod-from-Javascript-in-ASPNET.aspx

Related

Unable to redirect to another page using javascript while using jquery

I am unable to redirect to another after successful login through my webservice. I am getting correct response from web service But page doesn't redirect.
<script type="text/javascript">
function registerUser() {
try {
var username = document.getElementById("UserName");
var pwd = document.getElementById("Password");
$.ajax({
datatype: "json",
type: "POST",
url: "http://localhost:51290/CMSWebService.asmx/LoginUser",
data: "{'username':'" + username.value + "','pwd':'" + pwd.value + "'}",
async:false,
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("hello");
window.location.replace("default.aspx");// to redirect error occurs here
},
error: function (data) {
debugger;
if (data.d) {
}
}
});
}
catch (e) {
debugger;
alert(e);
}
}
</script>
Why don't you try window.location.replace(" full url of your page ") .
<script type="text/javascript">
function registerUser() {
try {
var username = document.getElementById("UserName");
var pwd = document.getElementById("Password");
$.ajax({
datatype: "json",
type: "POST",
url: "http://localhost:51290/CMSWebService.asmx/LoginUser",
data: "{'username':'" + username.value + "','pwd':'" + pwd.value + "'}",
async:false,
contentType: "application/json; charset=utf-8",
success: function (data) {
window.location = "./default.aspx";
alert("hello");
},
error: function (data) {
debugger;
if (data.d) {
}
}
});
}
catch (e) {
debugger;
alert(e);
}
}
</script>
Try this

ASP.NET MVC Controller - 500Error

Why isn't my controller function called?
I always get 500error (in fiddler). I get no error in Visual Studio or an error site.
Controller:
[POST("/test1")] // attributerouting (works with GET methods)
public ActionResult test1(TreeViewItemModel aItem)
{
...
}
Client:
var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
type: "POST",
url: "/test1",
data: tree,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
Some times 500 Internal Server Error occurred because of syntax error in the View of it's controller like { or } mismatch or etc. Did you check syntax of test1.cshtml?
problem was the data format:
solution:
public ActionResult test1(IEnumerable<TreeViewItemModel> aItem)
{
}
Client:
var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
type: "POST",
url: "/test1",
data: c,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});

ASP.NET hello world AJAX post

I keep getting a 500 with the following C#/jQuery. Any given implementation may not be right and thats not a huge issue. I'm just trying to get a hello world up. It works if the c# has no arguments but as soon as I try to recieve data it gives the 500.
[WebMethod]
public static string Test(string s)
{
// never gets here
}
$.ajax({
type: "POST",
url: "ajax.aspx/" + method,
/*async: true,*/
data: "{data:'" + data + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
callback(data.d);
}
});
latest attempt is this which still doesnt work:
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data: "data: {data:'abc'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("back");
}
});
I think you don't have to use MVC to make it work. I think the way you are passing json parameters are wrong. Please check below code and try and do let me know whether it works.
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data:'{"data":"abc"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
try this
[HttpPost]
public ActionResult Test(string x)
{
// never gets here
return Json(true)
}
$.ajax({
type: "Post",
url: "ajax/Test",
data: {x:'abc'},
dataType: "json",
success: function (data) {
alert("back");
}
});

Using Ajax in a .net project to call a .aspx.cs function NOT WORKING

Im using Ajax in a .net project (isn't MVC.net). I want to call a function of my .aspx.cs from a JScript Function.
This is my JScript code:
$("a#showQuickSearch").click(function () {
if ($("#quick_search_controls").is(":hidden")) {
$.ajax({
type: "POST",
url: "Default.aspx/SetInfo",
data: "{showQuickSearch}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert(response.d);
}
});
$("#quick_search_controls").slideDown("slow");
$("#search_controls").hide();
$("#search").hide();
} else {
$("#quick_search_controls").hide();
}
});
And this is my .aspx.cs Function:
[WebMethod]
public string SetInfo(string strChangeSession)
{
Label1.Text = strChangeSession;
return "This is a test";
}
The problem is that my .aspx.cs function is not being called and isn't updating the label.text.
Try making your function static.
[WebMethod]
public static string SetInfo(string strChangeSession)
{
//Label1.Text = strChangeSession; this wont work
return "This is a test";
}
data: "{showQuickSearch}" is not valid JSON.
Here's how a valid JSON would look like:
data: JSON.stringify({ strChangeSession: 'showQuickSearch' })
Also your PageMethod needs to be static:
[WebMethod]
public static string SetInfo(string strChangeSession)
{
return "This is a test";
}
which obviously means that you cannot access any page elements such as labels and stuff. It is inside your success callback that you could now use the result of the PageMethod to update some label or whatever.
$.ajax({
type: "POST",
url: "Default.aspx/SetInfo",
data: "{'strChangeSession':'showQuickSearch'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert(response.d);
},
error: function (xhr, status, error) {
var msg = JSON.parse(xhr.responseText);
alert(msg.Message);
}
});
And your backend code:
[WebMethod]
public static string SetInfo(string strChangeSession)
{
return "Response ";
}

JQuery and Ajax - Coming to Grips

I've read through several tutorials on the web about ajax posting with JQuery, all of them reference the response object from the web service as response / response.d -- This lead me to believe that this is the built in object for JQuery's response handler.
Code Snippet:
$('.submit').click(function () {
var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too
alert(theURL);
$.ajax({
type: "POST",
url: theURL,
data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this
contentType: "applications/json; charset=utf-8",
dataType: "json",
success: alert("Success: " + response.d), // this will change
failure: function (response) {
alert("Failure: " + response.d);
}
});
});
however the code is returning "Uncaught ReferenceError: response is not defined" in Chrome's Javascript console. What assumptions am I making that I need to re-evaluate.
You need to supply success with a function to execute:
success: function(response) {
alert(response.d);
}
Success (Like Failure) need a function to pass the response object through.
$('.submit').click(function () {
var theURL = document.location.hostname + ":" + document.location.port + "/LeadHandler.aspx/hello"; // this will change too
alert(theURL);
$.ajax({
type: "POST",
url: theURL,
data: "{'NameFirst':'" + $('#txtFirstName').val() + "'}", // again, change this
contentType: "applications/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("Success: " + response.d);
},
failure: function (response) {
alert("Failure: " + response.d);
}
});
});

Resources