Javascript 从.js文件调用C#方法
我有一个aspx页面,其中有方法Javascript 从.js文件调用C#方法,javascript,c#,Javascript,C#,我有一个aspx页面,其中有方法UpdateScreenAlertStatus() 我想要一个名为dtml.js的文件,其中包含函数openmodelpopup() 我想调用UpdateScreenAlertStatus()在javscript方法中openmodelpopup()使其成为web方法 [WebMethod] public static string UpdateScreenAlertStatus() {....} 请参阅MSDN文章 从javascript中,您可以通过Your
UpdateScreenAlertStatus()代码>
我想要一个名为dtml.js的文件,其中包含函数openmodelpopup()
我想调用UpdateScreenAlertStatus()代码>在javscript方法中openmodelpopup()
使其成为web方法
[WebMethod]
public static string UpdateScreenAlertStatus()
{....}
请参阅MSDN文章
从javascript中,您可以通过YourPage.aspx/UpdateScreenAlertStatus
或通过同一个aspx页面中的PageMethods
访问此文件。您可以使用以下javascript调用它
function openmodelpopup() {
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "YourPage.aspx/UpdateScreenAlertStatus",
success: (function (data) {
$("#statusDiv").text(data.d);
$("#statusDiv").show();
}),
error: (function () {
alert("Error occurred in server!");
})
});
}
我在这里假设,1。您的C#方法是static
并标记为WebMethod
。2.代码编译正确。3.您正在使用jQuery。4.javascript中的url是正确的。5.C#方法返回字符串状态。6.您正试图用该字符串更新html元素statusDiv
function MyMethod() {
$.ajax({
type: "POST",
url: "abc.aspx/UpdateScreenAlertStatus ",
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
在js函数openmodelpopup()中调用上述ajax
什么C#?你在使用网络表单吗?MVC?请指定从客户端代码调用服务器端方法的方法有多种,但它们都不是简单的方法调用。UpdateScreenAlertStatus
的作用是什么?从名称来看,您试图通过客户端代码调用的方法执行一些不可能执行的操作。UpdateScreenAlertStatus()是您想要在js中调用的代码隐藏方法吗?这就是您想要的吗?要从javascript调用C#方法,您需要使其能够通过http远程调用,很少有选项是[1]web api[2]mvc[3]aspx web方法[4]wcfyes UpdateScreenAlertStatus在我的代码中,我想在.js中调用,但是我使用的.js文件PageMethod抛出未定义的错误检查更新答案中的代码和条件。伙计们,运气不好,我在.js文件中添加了page方法,并调用了该方法,但它仍然从.js抛出未定义的错误file@user3675493也许你该走了发布了您的代码,以便我们可以看到您的错误操作函数OpenModalReplyDialog(url,SubscriberId){var returnValue=new ModalReturnValue(null,null,null);var retVal=null;var urlredirect=url+'/UC_5_0_0.aspx?SubscriberId='+SubscriberId;PageMethods.set_path(urlredirect);PageMethods.UpdateScreenAlertStatus(XtTypes.EnumScreenAlertStatusType.Acknowed);returnValue=OpenModel(URLDirect,350500);return true;}这是.js文件
[WebMethod]
public static void UpdateScreenAlertStatus()
{....}