How do I do an ajax post and have a master page webmethod handle it?
I am developing a set of new pages for an existing (and large) web app and am making them much more HTML and JQuery-centric than we've done before. For example, there are NO server controls and I am not using ViewState - everything is done in standard HTML. I've created the first page and it works great. It has lots of explicit ajax calls into static methods on the ASPX code-behind class to get or store data and these work fine.
Before I branch out to the remaining pages, I am placing a fair amount of the page into a Master page and the callbacks for these components into the Master page code-behind. However, any function call to a static method on the master page is failing. I could just move these methods to an ASMX web service page but now my curiosity is piqued and I'd like to understand why calls to methods in the aspx page work but calls to methods defined on the master page do not.
This is not an issue of identifying DOM elements (most of the questions around this topic are about DOM element access). It is a question about why an AJAX callback to "SaveWeightData" works when the method is defined on the page but fails when the method is defined on the master page:
$.ajax({
type: "POST",
url: "<%=PageName%>/SaveWeightData",
data: "{'we开发者_高级运维ightItem':" + JSON.stringify(newWeight) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("Success!");
},
error: function (xhr, ajaxOptions, thrownError, request, error) {
alert('xrs.status = ' + xhr.status + '\n' +
'thrown error = ' + thrownError + '\n' +
'xhr.statusText = ' + xhr.statusText + '\n' +
'request = ' + request + '\n' +
'error = ' + error);
}
})
Thanks!
A master page becomes a UserControl
at runtime under the Page
object. UserControls
cannot host WebMethods
.
精彩评论