I write a countdown timer in jQuery and i need to keep some datas in a session when countdown ends. Datas have to be sent to server to update. How can i handle this sit开发者_运维问答uation. I am new in jQuery and asp.net so could you explain this briefly
Supose you have a class like this:
public class MyClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
And an action method like this:
public JsonResult Test(string data)
{
// do something with data
var obj = new MyClass {FirstName = "Thiago", LastName = "Temple"};
return Json(obj);
}
Then you can write a js function using jquery like this:
$.get('<%= Html.Action("Test", "MyController")', {data: "some value"}, function(result){
alert(result.FirstName + " " + result.LastName);
});
I hope this example is useful.
Try using a PageMethod. You can create it in your codebehind and call it with jQuery. It's really fast & and great for countdown timers.
Make sure you enable PageMethods in your scriptmanager.
Call ASP.NET PageMethod/WebMethod with jQuery - returns whole page
If it's a one-way communication probably the quickest way is to call an aspx page using the $.get method:
http://api.jquery.com/jQuery.get/
something like this:
$.get('mypage.aspx', myData , function(result) {
alert('Ok');
});
where myData is a JSON object.
for more specific needs you can use the $.ajax method.
It will be two-way communication both client and server have to communicate each other.
精彩评论