How to post data without page refresh in ASP.Net using AJAX or Jquery
I have been working on a web application, I used to work in PHP with jQuery and it works well, but now I have moved on it, but it's not working开发者_如何学Go fine with the same logic of jQuery which I have used for posting a form. May be ASP has some special and more easier way for page posting and getting response without page refresh, does anybody know about that?
You can use jQuery post function with form serialize function to post a form without refresh,
$.post($("#formId").serialize(), function(data) {
$('#result').html(data);
});
You can use $.ajax.
For example:
$.ajax({
type: "POST",
url: "/some/url",
data: "name=John&location=Boston",
dataType: "json",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
See the following link:
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Don't avoid a web service just because it sounds complicated; in ASP.NET it's not complicated at all to set up an endpoint to communicate via JSON*. In fact, if you use "page methods", you can define the service method inside your ASPX page's code behind.
Whatever you do, avoid calling an ASPX page directly for JSON like you might in PHP. In ASP.NET, requests to ASPX pages run through a request pipeline, instantiate an instance of the Page class, and run through the Page's life cycle. All of that processing is targeted toward requests that are handling server-side events and building an HTML document based on ASP.NET web controls, user controls, etc - a process which is not desirable when you just want some lightweight JSON communication.
(*) There's an update coming soon that will fixes WCF for the most part, but do avoid WCF for the time being. For what you're trying to do, ASMX works well and is vastly less complex than WCF.
精彩评论