如何从类函数中获取Ajax响应?

时间:2014-07-23 12:17:29

标签: c# javascript jquery asp.net ajax

我有一节课: -

 namespace CodeFiles
 {
 public class GetDetails
 {

      public List<string> AllItems(string value)
      {
            string[] items =  { "hassaan", "samina", "noureen", "hafsa", "wajeeh", "farnaz", "basim", "shahnaz" };
            var lst = new List<string>();
            return (from o in items
                    where o.Contains(value)
                    select o).ToList();
      } 
   }
}

我只想使用ajax响应中的AllItem方法。但是想知道如何应用它。

我的ajax电话就像: -

$(document).ready(function () {
    $('#<%= txtName.ClientID%>').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "CodeFiles.GetDetails.cs/AllItems",
                data: "{'value': '" + request.term + "' }",
                type: "POST",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (result) {
                    response(result.d);
                },
                error: function (result) {
                    alert('There is a problem processing your request');
                }
            });
        }
    })
});

我在url: "CodeFiles.GetDetails.cs/AllItems"收到错误导致错误。 如何实现这一点。

2 个答案:

答案 0 :(得分:0)

你不能将类名作为url传递。您只需传递ajax响应文件的URL。

答案 1 :(得分:0)

这是一个网络应用程序吗?

如果是这种情况,请创建一个webform或如果已经存在,则只需要使用[WebMethod]创建一个静态方法装饰,然后在方法内部初始化该类并调用方法AllItems。

简单示例:

在Default.aspx上:

[WebMethod]
public static string Hello(string name)
{
    return name;
}

然后在你的js:

function Hello() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/Hello",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            response(result.d);
        },
        error: function (result) {
            alert('There is a problem processing your request');
        }
    });
}