[英]ASP.Net call codebehind on $.ajax success
I have a page (default.aspx) with codebehind. 我有一个隐藏代码的页面(default.aspx)。 The a$.ajax url is getting a response from one place, and on its success I want to call the codebehind function.
a $ .ajax url从一个地方得到响应,成功后,我想调用codebehind函数。
(In case I made a mistake while obfuscating the code, the $.ajax works perfectly and I get the desired response). (以防我在混淆代码时犯了一个错误,$。ajax可以完美地工作,并且可以得到所需的响应)。
How is this possible? 这怎么可能?
Code I'm using: 我正在使用的代码:
jQuery.support.cors = true; // force cross-site scripting (as of jQuery 1.5)
$.ajax({
type: "POST",
url: URL,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var resultCount = response.d
alert("*** RESULT ****" + resultFields);;
var string = StringReturn(); // the codebehind
alert(string);
},
error: function (e) {
alert("Unavailable");
}
});
Codebehind: 代码背后:
[WebMethod]
protected static string StringReturn()
{
return "StringReturn() success";
}
However, I'm getting error messages saying that StringReturn isn't a valid function. 但是,我收到错误消息,说StringReturn不是有效的函数。
Any help would be appreciated? 任何帮助,将不胜感激?
I've added the following lines of code to the page as advised: 我已按照建议将以下代码行添加到页面中:
<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true"> </asp:ScriptManager>
I've also changed the code to call a javascript function on the Success event, the function being: 我还更改了代码,以在成功事件上调用javascript函数,该函数为:
function HelloWorlds() {
alert("HelloWorld() method");
message = PageMethods.StringReturn();
message.toString();
alert(message);
}
however that doesn't appear to work. 但是,这似乎不起作用。 What am I missing?
我想念什么?
您需要在页面上有一个scripmanager,然后您可以像这样调用它PageMethods.StringReturn()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.