从ASP.NET中的Javascript调用服务器端功能 [英] Call Server side function from Javascript in ASP.NET
本文介绍了从ASP.NET中的Javascript调用服务器端功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从asp.net中的javascript函数调用服务器端函数
谢谢,
Hi,
I want to call Server side function from the javascript function in asp.net
Thanks,
推荐答案
最好改用AJAX.它简单易实现.虽然
您可以使用javascript中的__dopostback发起回发..
Its better to use AJAX instead.Its simple and easy to implement. Although
you can initiate a postback using __dopostback from javascript..
现在在ASP.NET中执行此操作的标准"方法是使用AJAX.可以使用MS AJAX完成此操作,但我更喜欢使用jQuery的Ajax调用.
您要做的是在ASP.NET项目中创建一个ScriptService
,将服务器端代码添加为WebMethod
,然后从您的JavaScript中调用它.如果您不想添加ScriptService
,则可以将WebMethod
添加到Page
.这是来自jQuery的典型ajax调用:
The "standard" way to do this in ASP.NET now, is to use AJAX. It''s possible to use MS AJAX to accomplish this, but I prefer to use jQuery''s Ajax calls.
What you do is create aScriptService
in your ASP.NET project, add the serverside code as aWebMethod
, and then call it from your javascript. If you don''t want to add aScriptService
, you can add theWebMethod
to aPage
. Here''s a typical ajax call from jQuery:
.ajax({ type: "POST", url: "MyScriptService.asmx/GetDate", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); } });
.ajax({ type: "POST", url: "MyScriptService.asmx/GetDate", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); } });
这篇关于从ASP.NET中的Javascript调用服务器端功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文