如何从javascript中调用c#中的任何方法? [英] how can call any method in c# from javascript ?

查看:89
本文介绍了如何从javascript中调用c#中的任何方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从javascript中调用c#中的任何方法???

解决方案

我认为你是想从javascript调用服务器端函数,是吗? br />
然后谷歌会帮助你很多: https://www.google.de/?gws_rd=ssl#q=calling+server+side+function+from+javascript [ ^ ]



这看起来很有希望的链接: http ://www.c-sharpcorner.com/uploadfile/rohatash/calling-server-side-function-from-javascript-in-asp-net/ [ ^ ]



您可能还想查看代码属性 [WebMethod]



干杯!


你从来没有真正从Javascript调用任何服务器端方法,Javascript总是在客户端被浏览器调用。 (我不计算服务器端Javascript的罕见情况 - 如果你标记了ASP.NET,你很难使用它。)



所有你能做的就是向服务器发送一些HTTP请求并获得HTTP响应。此类请求中使用的URL指向的资源应编程为解释此类请求并提供适当的响应。从Javascript开始,它使用Ajax完成: http://en.wikipedia.org/wiki/Ajax_(programming) [ ^ ]。



这样做的一个非常方便的方法是jQuery Ajax: http://api.jquery。 com / jquery.ajax [ ^ ]。



如果您需要学习jQuery(强烈推荐),请参阅:

http://en.wikipedia.org/wiki/JQuery [ ^ ],

http://jquery.com [ ^ ],

http://learn.jquery.com [ ^ ],

http://learn.jquery.com/using-jquery-core [ ^ ],

http://learn.jquery.com/about-jquery/how-jquery-works [ ^ ](从这里开始)。



-SA


使C#方法静态并用[WebMethod]属性标记它

像这样: -



[WebMethod]

public static void TestMethod()

{

}



javascript code

=================

 <   script  >  
function test(){
alert(PageMethods.TestMethod());
}
< / script >


how can call any method in c# from javascript ???

解决方案

I presume you're looking to call a server side function from javascript, yes?
Then google will help you a lot: https://www.google.de/?gws_rd=ssl#q=calling+server+side+function+from+javascript[^]

This looks like a promising link: http://www.c-sharpcorner.com/uploadfile/rohatash/calling-server-side-function-from-javascript-in-asp-net/[^]

You might also want to look into the code attribute [WebMethod].

Cheers!


You never ever really call any server-side method from Javascript, which is always called on the client side, by a browser. (I don't count rare cases of server-side Javascript — you hardly use it if you have tagged "ASP.NET").

All you can do is to send some HTTP request to the server and get HTTP response. The resource pointed by the URL used in such request should be programmed to interpret such request and provide appropriate responses. From the Javascript, it is done using Ajax: http://en.wikipedia.org/wiki/Ajax_(programming)[^].

A very convenient way of doing so is jQuery Ajax: http://api.jquery.com/jquery.ajax[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com[^],
http://learn.jquery.com[^],
http://learn.jquery.com/using-jquery-core[^],
http://learn.jquery.com/about-jquery/how-jquery-works[^] (start from here).

—SA


Make C# method Static and Tag it with [WebMethod] attribute
like this :-

[WebMethod]
public static void TestMethod()
{
}

javascript code
=================

<script>
    function test(){
        alert(PageMethods.TestMethod());
    }
</script>


这篇关于如何从javascript中调用c#中的任何方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆