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

时间:2022-10-09 14:58:49

I have a class :-

我上课了: -

 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();
      } 
   }
}

All i want to use AllItem method from ajax response. But wonder how to apply this.

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

My ajax call is like :-

我的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');
                }
            });
        }
    })
});

I am getting error at url: "CodeFiles.GetDetails.cs/AllItems" is causing bug. How to implement this.

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

2 个解决方案

#1


0  

you can not pass class name as url. You have to pass only the url of the ajax response file.

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

#2


0  

This is a web application right?

这是一个Web应用程序吗?

If this is the case, make an webform or if this already exist, you only need to create a static method decorate with [WebMethod] then inside the method init the class and call the method AllItems.

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

Simple example:

On Default.aspx:

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

Then in your js:

然后在你的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');
        }
    });
}

#1


0  

you can not pass class name as url. You have to pass only the url of the ajax response file.

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

#2


0  

This is a web application right?

这是一个Web应用程序吗?

If this is the case, make an webform or if this already exist, you only need to create a static method decorate with [WebMethod] then inside the method init the class and call the method AllItems.

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

Simple example:

On Default.aspx:

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

Then in your js:

然后在你的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');
        }
    });
}