简体   繁体   English

如何从JS调用服务器端方法

[英]How to call a server side method from JS

I've read many questions, forums, blogs, tried many things and I just can't seems to make it work. 我已经阅读了很多问题,论坛,博客,尝试了很多东西,我似乎无法使其发挥作用。

I've tried using PageMethods.MyMethod() and that didn't work. 我尝试过使用PageMethods.MyMethod() ,但是没有用。 Made sure my ScriptManager had EnablePageMethods ="true" and still nothing. 确保我的ScriptManager具有EnablePageMethods ="true" ,但仍然没有。 I have a breakpoint on the server side and it never hits. 我在服务器端有一个断点,它永远不会命中。 Tried using ajax and still nothing 尝试使用ajax仍然没有

I'm 1st trying to understand how to make it work to then implemented on my program. 我是第一个尝试了解如何使其工作,然后在我的程序上实现。

This is what I've tried so far: 这是我到目前为止所尝试的:

Server-side: 服务器端:

[System.Web.Services.WebMethod]
    public static void SomeMethod(string subject, string body, string recipients, string CurrentUserId)
    {
        MessageBox.Show("In c#");
    }

JS: JS:

function SomeFuntion()
{
        debugger; alert("Before web service");

        //PageMethods.CreateDraft(var1, var2, var3, var4);

        $.ajax
            (
                {
                    type: "POST",
                    url: "NewMessage.aspx/SomeMethod",
                    data: "{subject:'" + var1+ "', body:'" + var2+ "', recipients:'" + var3
                        + "', CurrentUserId:'" + var4+ "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success:
                        function()
                        {
                            alert("In ajax");
                        }
                }
            );
}

As you can see, I tried PageMethods and it didn't work. 正如您所看到的,我尝试了PageMethods并且它无法正常工作。 I know the function runs cause I see the alert message. 我知道函数运行导致我看到警报消息。 By the way, the function is being called when the onclick event is fired of on a button. 顺便说一句,当在按钮上触发onclick事件时,将调用该函数。 I don't get any errors and neither does it hit the break point on the MessageBox . 我没有得到任何错误,也没有达到MessageBox的断点。 This is new to me so any explanation would be very helpful too. 这对我来说是新的,所以任何解释都会非常有用。

Check for errors from the server: 检查服务器的错误:

function SomeFuntion()
{

    $.ajax({
            type: "POST",
            url: "NewMessage.aspx/CreateDraft",
            data: "{subject:'" + var1+ "', body:'" + var2+ "', recipients:'" + var3+ "', CurrentUserId:'" + var4+ "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend:function () 
            {
                 alert("about to send request");
            },
            success: function()
            {
                        alert("In ajax");
            },
            error: function(xhr, status, error) 
            {
                   var err = eval("(" + xhr.responseText + ")");
                   alert(err.Message);
            }
        );
}

more additions Since you are telling me that you are getting a "The HTTP verb POST used to access path '...' is not allowed" you might need configuration to enable .NET web service for http POST requests. 更多新增内容因为您告诉我您正在获取“用于访问路径的HTTP动词POST'...'是不允许的”您可能需要配置为http POST请求启用.NET Web服务。

I have dropped .NET few years ago and therefore I have no direct experience but from what I could find in other sites you might need configuration at the server level such: 几年前我已经删除了.NET,因此我没有直接经验,但是从其他站点中可以找到的内容中,您可能需要在服务器级别进行配置,例如:

<system.web>
   <webServices>
     <protocols>
       <add name="HttpPost"/>
     </protocols>
   </webServices>
 </system.web>

Or directives above your method such 或者你方法之上的指令

[ScriptMethod(UseHttpPost = true)]
public string SomeMethod()
{
    ....
}

I managed to figure it out with the help of a friend, I would post my answer but I believe it's quite long and complicated. 我设法在朋友的帮助下弄清楚了,我会发布我的答案,但我相信这很长很复杂。 I understood most of the things he did but I still didn't believe how it worked. 我理解他所做的大部分事情,但我仍然不相信它是如何运作的。 It was pretty much almost the same thing I did but he made the JS in a separate file and set that file like a base JS. 这几乎和我做的几乎一样,但是他把JS放在一个单独的文件中并将该文件设置为基础JS。 Called the web service there and from my JS he called that function that calls the web service. 在那里调用了Web服务,从我的JS中调用了调用Web服务的函数。 And it worked lol 它工作大声笑

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

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