简体   繁体   English

从JavaScript调用全局类的C#函数

[英]Call c# function of global class from javascript

I have a global function that returns some string. 我有一个返回一些字符串的全局函数。 I need to access to that function from JavaScript in one of the pages and set returned value to JavaScript variable. 我需要从页面之一中的JavaScript访问该函数,并将返回值设置为JavaScript变量。

Example:- 例:-

var jsvariable = <%GlobalClass.MethodReturningString();%>;

How to do that? 怎么做?

You cannot call C# function like this. 您不能像这样调用C#函数。 you need to create a web service or webmethod in order to call this function. 您需要创建一个Web服务或Web方法才能调用此函数。 Please see this link it will help you. 请查看此链接,它将为您提供帮助。 http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx

Perform the following steps : 执行以下步骤:

  1. Right click on your website or web application and add a WebHandler. 右键单击您的网站或Web应用程序,然后添加一个WebHandler。 The extension for the webhandler is '.ashx'. Webhandler的扩展名是“ .ashx”。 Let's say you name your handler as 'Handler.ashx' 假设您将处理程序命名为“ Handler.ashx”
  2. Open the Handler.ashx file and within the ProcessRequest method write the following : context.Response.Headers.Clear(); context.Response.Write(GlobalClass.MethodReturningString();); context.Response.Flush(); 打开Handler.ashx文件,并在ProcessRequest方法中编写以下内容: context.Response.Headers.Clear(); context.Response.Write(GlobalClass.MethodReturningString();); context.Response.Flush(); context.Response.Headers.Clear(); context.Response.Write(GlobalClass.MethodReturningString();); context.Response.Flush();
  3. In your javascript, perform an ajax call using jQuery : 在您的JavaScript中,使用jQuery执行ajax调用:

    $.ajax({ url:'someUrl/Handler.ashx', type:'GET', success : function(data){ var someJavascriptVariable = data; } });

NOTE: This is a quick and dirty write so I'm not guaranteeing that the code will surely work, but it's a point for you to start with. 注意:这是一个快速而肮脏的编写,因此我不保证该代码一定能正常工作,但这是您入门的重点。

May i thick You cannot do this but u can access ur c# function using ajax 我可以厚一点吗,但是您可以使用Ajax访问ur c#函数

For Example 例如

   $.post('url', {parameter : parameter }, function (result) {

         // here the result is your function return value or output

   },"json");

URL FORMAT : '/pageurl/methodname' 网址格式:“ / pageurl /方法名”

First of all, as a clarification, it seems that you're not trying to actually call a C# method from Javascript, but rather render the return from a C# method inside the page so that it can be used as a Javascript variable value on the client side. 首先,为澄清起见,您似乎并不是在试图从Javascript实际调用C#方法,而是在页面内呈现C#方法的返回值,以便可以将其用作Javascript变量值。客户端。

In order to do that you need to update you syntax like: 为此,您需要更新语法,例如:

var jsvariable = '<%= GlobalClass.MethodReturningString() %>';

Please note that if your class is not in the same namespace as the inherited page from the code behind file, then you need to import its namespace like below: 请注意,如果您的类与从文件后面的代码继承的页面不在同一个命名空间中,则您需要如下导入其命名空间:

<%@ Import Namespace="GlobalClassNamaspace" %>

The namespace import can also be done globally (and it will be automatically available in all of the site's pages), using the web.config file as described here . 命名空间导入也可以使用此处所述的web.config文件在全球范围内完成(并且将自动在网站的所有页面上提供)。

If you were to actually need to call a C# method from Javascript, which would have been needed if you wanted to dynamically use its results from client side code, then that could be accomplished through Ajax . 如果您实际上需要从Javascript调用C#方法,而您想通过客户端代码动态使用其C#方法,则可以通过Ajax实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM