简体   繁体   English

执行对ASP.NET WEb应用程序的AJAX调用

[英]To execute the AJAX call to ASP.NET WEb Application

Below is my code in code behind in ASP.NET Web Application 下面是我在ASP.NET Web应用程序背后的代码中的代码

public string GetData(string name)
    {
        WCF_Web_Service.Service1 client = new WCF_Web_Service.Service1();
        string Name=client.GetData(name);
        return Name;
    }

Here I have consumed my simple WCF Service should hive output as Hello, 在这里,我已经消耗了我的简单WCF服务应该将输出配置为Hello,

and below is the my jquery code 以下是我的jQuery代码

function asyncServerCall(name) {
        jQuery.ajax({
            url: 'WebForm1.aspx/GetData',
            type: "POST",
            data: "{'name':" + name + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data.d);
            }
        });
    }

I have called this function in click event of button as follows 我在按钮的单击事件中调用了此函数,如下所示

<input type="button" value="click me" onclick="asyncServerCall(1);" />,

I wanted to make AJAX call to call this method from WCF Service, when I click button I get following error 我想通过WCF服务进行AJAX调用来调用此方法,当我单击按钮时出现以下错误

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

I am novice to use WCF Service and to make AJAx call to evoke its method, any help will be greatly appreciated.... 我是新手,无法使用WCF服务并进行AJAx调用以唤起其方法,将不胜感激。

Your GetData method needs to be a static web method, so change it to this. 您的GetData方法必须是静态网络方法,因此请将其更改为该方法。

[System.Web.Services.WebMethod]
public static string GetData(string name)
{
    WCF_Web_Service.Service1 client = new WCF_Web_Service.Service1();
    string Name=client.GetData(name);
    return Name;
}

Here's a good article on the why's but essentially it's because: 这是一篇关于为什么的好文章,但本质上是因为:

This is exactly why they must be marked as static. 这就是为什么必须将它们标记为静态的原因。 They cannot interact with the instance properties and methods of your Page class, because a page method call creates no instance of the Page or any of its controls. 它们无法与Page类的实例属性和方法进行交互,因为页面方法调用不会创建Page实例或其任何控件。

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

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