简体   繁体   English

从Javascript调用C#方法

[英]Call C# method from Javascript

if(httpRequest.readyState == 4 && httpRequest.status == 200)
{
    <% SetText(); %>
}

I need to call method from code behind in javascript. 我需要从javascript后面的代码中调用方法。 Method is obviously called SetText() and it's void. 方法显然称为SetText() ,它是无效的。

This I tried didn't work. 我尝试过的这个没有用。

You have to expose the C# method using a service or a handler. 您必须使用服务或处理程序公开C#方法。 Depending on your architecture the handler can be implemented in different ways (MVC, .net HttpHandler, JayRock handler, etc.). 根据您的体系结构,处理程序可以以不同的方式实现(MVC,.net HttpHandler,JayRock处理程序等)。 And then you have to call the method from Javascript using an AJAX request. 然后,您必须使用AJAX请求从Javascript调用该方法。

And remember, avoid to embed CSharp code blocks inside server side generated Javascript code. 记住,请避免在服务器端生成的Javascript代码中嵌入CSharp代码块。

References: 参考文献:

It is not possible, at least if you don't make an AJAX request. 这是不可能的,至少如果您不提出AJAX请求。 Otherwise, it's impossible to access server-side methods via client-side(in this case via JS). 否则,不可能通过客户端(在这种情况下是通过JS)访问服务器端方法。

H i answered a similar question on Calling a C# method from Javascript I provided an example using AJAX: Call C# method in javascript function directly 我回答了有关从Javascript调用C#方法的类似问题,我提供了一个使用AJAX的示例: 直接在javascript函数中调用C#方法

Hope this will help. 希望这会有所帮助。

The client is incapable of accessing server methods in the manner in which you specified in your question. 客户端无法以您在问题中指定的方式访问服务器方法。 What you want to do is not possible unless you use something like AJAX. 除非您使用AJAX之类的方法,否则您想做的事情是不可能的。

Please explain what you're trying to do, perhaps someone can explain a different way to solve your problem. 请说明您要执行的操作,也许有人可以说明其他方法来解决您的问题。

Make one ajax call, there you check whether the connection is ok or not. 拨打一个ajax电话,然后在此处检查连接是否正常。 If connection is ok make the actual call there itself(in server side) and return data. 如果连接正常,则在服务器端本身(在服务器端)进行实际调用并返回数据。

Since you made an async request you page data is not going to show any error message. 由于您发出了异步请求,因此页面数据将不会显示任何错误消息。 Even if there is some error you will get in error handler and you know what to do when error occur. 即使出现一些错误,您也会进入错误处理程序,并且知道发生错误时该怎么办。

There should not be any scenario when the client code has to contact server method directly. 当客户端代码必须直接联系服务器方法时,应该没有任何情况。 You can always do work around with that in case your design is like that. 如果您的设计是这样的,您总是可以解决。

You can accomplish this using AJAX. 您可以使用AJAX完成此操作。

I'm currently using a HTTPHandler to make my calls via AJAX. 我目前正在使用HTTPHandler通过AJAX进行呼叫。 I'm accessing my handler using a service ".asmx" with code the looks like this 我正在使用服务“ .asmx”访问我的处理程序,其代码如下所示

<%@ WebService Language="C#" Class="MyClass" %>

try this link http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/ 试试这个链接http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/

yes but it is not possible without ajax call :(. 是的,但是没有ajax调用:(是不可能的。

And please make sure you have imported all the necessary jquery files and the function that you have written for ajax call should be a "STATIC". 并且请确保已导入所有必需的jquery文件,并且为ajax调用编写的函数应为“ STATIC”。

rest will be cleared please follow the link 其余的将被清除,请点击链接

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

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