rich brown
asked on
How do I pass JavaScript Object to my WebMethod on my aspx.cs using ASPX webforms?
I can pass javascript with 1 arg to WebMethod on my aspx webform but I am unable to pass javascript object to my codebehind WebMethod. What's can I do to pass a Object to my aspx.cs WebMethod? Get get the 500 status error when I click the btn.
This work.....
//==============
My ajax1:
//===============
function Show1() {
$.ajax({
type: "POST",
url: "MyPage.aspx/GetCurrentTim e",
data: '{name: "' + document.getElementById("t xtUserName ").value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
My Web Method:
[WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}
This does not work....
//==============
My ajax2:
//===============
function Show2() {
var val = { FirstName: "Mark", LastName: "Jones" };
var iParam = { Info: val };
var iUrl = "MyPage.aspx/Test";
$.ajax({
url: iUrl,
data: JSON.stringify(iParam),
contentType: "application/json; charset=utf-8",
type: 'POST',
dataType: 'html',
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
//==============
My Web Method2:
//===============
[WebMethod]
public static string Test(TestMe info)
{
var i = info;
string msg = string.Empty;
return msg;
}
//===============
My class is:
//===============
public class TestMe
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
This work.....
//==============
My ajax1:
//===============
function Show1() {
$.ajax({
type: "POST",
url: "MyPage.aspx/GetCurrentTim
data: '{name: "' + document.getElementById("t
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
My Web Method:
[WebMethod]
public static string GetCurrentTime(string name)
{
return "Hello " + name + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}
This does not work....
//==============
My ajax2:
//===============
function Show2() {
var val = { FirstName: "Mark", LastName: "Jones" };
var iParam = { Info: val };
var iUrl = "MyPage.aspx/Test";
$.ajax({
url: iUrl,
data: JSON.stringify(iParam),
contentType: "application/json; charset=utf-8",
type: 'POST',
dataType: 'html',
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
//==============
My Web Method2:
//===============
[WebMethod]
public static string Test(TestMe info)
{
var i = info;
string msg = string.Empty;
return msg;
}
//===============
My class is:
//===============
public class TestMe
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you welcome!
ASKER