Help me solving my problematic issue, i have an small web app using jquery json and webmethod to get and update data but when i debug javascript by firebug, the code $.ajax do not run although i put it in $(document).ready(function(){});
Here is my code :
<script type="text/javascript">
$(document).ready(function () {
var get = function getAbout() {
$.ajax({
type: "POST",
url: "AboutManagement.aspx/getAbout",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var aboutContent = response.d;
alert(aboutContent);
$('#title-en').val(aboutContent.Name);
$('#title-vn').val(aboutContent.NameVn);
$('#content-en').val(aboutContent.Description);
$('#content-vn').val(aboutContent.DescriptionVn);
$('#id').val(aboutContent.CategoryId);
},
failure: function (message) {
alert(message);
},
error: function (result) {
alert(result);
}
});
}
var update = function updateAbout() {
var abt = {
"CategoryId": $('#id').val(),
"Name": $('#title-en').val(),
"NameVn": $('#title-vn').val(),
"Description": $('#content-en').val(),
"DescriptionVn": $('#content-vn').val()
};
$.ajax({
type: "POST",
url: "AboutManagement.aspx/updateAbout",
data: JSON.stringify(abt),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var aboutContent = response.d;
$('#title-en').val(aboutContent.Name);
$('#title-vn').val(aboutContent.NameVn);
$('#content-en').val(aboutContent.Description);
$('#content-vn').val(aboutContent.DescriptionVn);
},
failure: function (message) {
alert(message);
},
error: function (result) {
alert(result);
}
});
}
getAbout();
$('#SaveChange').bind('click', function () { updateAbout(); return false; });
$('#Reset').bind('click', function () { getAbout(); return false; })
});
</script>
Server code :
public Entities.Category getAbout()
{
Entities.Category about = new Entities.Category();
using (var context = new CarCareCenterDataEntities())
{
about = (from c in context.Categories where c.Type == "About" select c).SingleOrDefault();
}
return about;
}
public Entities.Category updateAbout(Entities.Category updateAbout)
{
try
{
Entities.Category about = new Entities.Category();
using (var context = new CarCareCenterDataEntities())
{
context.Categories.ApplyCurrentValues(updateAbout);
context.SaveChanges();
about = (from c in context.Categories where c.Type == "About" select c).SingleOrDefault();
}
return about;
}
catch
{return null;}
}
Server code WEB METHOD :
[WebMethod]
public static Entities.Category getAbout()
{
return getAbout();
}
[WebMethod]
public static bool updateAbout(Entities.Category about)
{
return updateAbout(about);
}
My web methods are placed in file .ascx and insert file .asxc into web page and write jquery code to using web method in webpage. but nothing occurs even error. I try to debug but my function get and update do not run even put it in $(document).ready . any
ideas ?
In webmethod you cannot declare the argument in function of type custom class or entity. You need to pass the single type of object.
Refer example below:
In place of this code,
[WebMethod]
public static bool updateAbout(Entities.Category about)
Use below format
Use below format
[WebMethod]
public static bool updateAbout(object CategoryId, object Name, object NameVn, object Description, object DescriptionVn)
If you problem is not solved please reply
Regards,
Chirag Vidani | My Blog MCTS - .Net Framework 4.0, Web Applications
Ne7ven
Member
108 Points
130 Posts
Can not run Jquery code in ASP.NET
Sep 13, 2011 03:58 AM|LINK
Hi all,
Help me solving my problematic issue, i have an small web app using jquery json and webmethod to get and update data but when i debug javascript by firebug, the code $.ajax do not run although i put it in $(document).ready(function(){});
Here is my code :
<script type="text/javascript"> $(document).ready(function () { var get = function getAbout() { $.ajax({ type: "POST", url: "AboutManagement.aspx/getAbout", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var aboutContent = response.d; alert(aboutContent); $('#title-en').val(aboutContent.Name); $('#title-vn').val(aboutContent.NameVn); $('#content-en').val(aboutContent.Description); $('#content-vn').val(aboutContent.DescriptionVn); $('#id').val(aboutContent.CategoryId); }, failure: function (message) { alert(message); }, error: function (result) { alert(result); } }); } var update = function updateAbout() { var abt = { "CategoryId": $('#id').val(), "Name": $('#title-en').val(), "NameVn": $('#title-vn').val(), "Description": $('#content-en').val(), "DescriptionVn": $('#content-vn').val() }; $.ajax({ type: "POST", url: "AboutManagement.aspx/updateAbout", data: JSON.stringify(abt), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var aboutContent = response.d; $('#title-en').val(aboutContent.Name); $('#title-vn').val(aboutContent.NameVn); $('#content-en').val(aboutContent.Description); $('#content-vn').val(aboutContent.DescriptionVn); }, failure: function (message) { alert(message); }, error: function (result) { alert(result); } }); } getAbout(); $('#SaveChange').bind('click', function () { updateAbout(); return false; }); $('#Reset').bind('click', function () { getAbout(); return false; }) }); </script>Server code :
public Entities.Category getAbout() { Entities.Category about = new Entities.Category(); using (var context = new CarCareCenterDataEntities()) { about = (from c in context.Categories where c.Type == "About" select c).SingleOrDefault(); } return about; } public Entities.Category updateAbout(Entities.Category updateAbout) { try { Entities.Category about = new Entities.Category(); using (var context = new CarCareCenterDataEntities()) { context.Categories.ApplyCurrentValues(updateAbout); context.SaveChanges(); about = (from c in context.Categories where c.Type == "About" select c).SingleOrDefault(); } return about; } catch {return null;} }Server code WEB METHOD :
[WebMethod] public static Entities.Category getAbout() { return getAbout(); } [WebMethod] public static bool updateAbout(Entities.Category about) { return updateAbout(about); }My web methods are placed in file .ascx and insert file .asxc into web page and write jquery code to using web method in webpage. but nothing occurs even error. I try to debug but my function get and update do not run even put it in $(document).ready . any ideas ?
chiragvidani
Participant
1288 Points
254 Posts
Re: Can not run Jquery code in ASP.NET
Sep 13, 2011 05:19 AM|LINK
Hello,
In webmethod you cannot declare the argument in function of type custom class or entity. You need to pass the single type of object.
Refer example below:
In place of this code, [WebMethod] public static bool updateAbout(Entities.Category about) Use below format Use below format [WebMethod] public static bool updateAbout(object CategoryId, object Name, object NameVn, object Description, object DescriptionVn) If you problem is not solved please replyChirag Vidani | My Blog
MCTS - .Net Framework 4.0, Web Applications
"…Mark As Answer" if my reply is helpful to you…”