需要帮助诊断此Ajax请求调用

时间:2018-06-30 06:52:29

标签: javascript c# asp.net ajax

需要帮助诊断此Ajax请求调用。

过滤后的文本是来自页面上方输入框的文本框,filters是布尔值,用于检查用户是否要搜索任何内容,过滤后的文本是用户希望过滤的内容。

function GetData(torf) {
        var watever = { "withfilters": torf, "filteredtext": $('#SortOrdersBy2').val() };

        $.ajax({
            url: '/WebService.asmx/GetData',
            method: 'post',
            dataType: 'json',
            data: JSON.stringify(watever),
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                var employeeTable = $('#datatable tbody');

                var employees = data;

                for (i = 0; i < employees.length; i++) {
                    employeeTable.append('<tr><td>' + employees[i].CustomerId + '</td><td>' + employees[i].CustomerName + '</td><td>' + employees[i].Engineer + '</td></tr>');
                }
            },
            error: function (err) {
                alert("ERROR");
            }
        });
    }

它转到此Web服务代码并在加载时运行它,但仍显示错误功能。

[WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public string GetData(bool withfilters, string filteredtext)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString;
        List<Data1> Data = new List<Data1>();
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            SqlCommand query = new SqlCommand
                (withfilters ? "SELECT * FROM MainTable CustomerId LIKE '%" + (filteredtext) + "%' OR CustomerName LIKE '%" + (filteredtext) + "%' OR Engineer LIKE '%" + (filteredtext) + "%')"
                             : "SELECT * FROM MainTable",con);
            SqlDataReader rdr = query.ExecuteReader();

            while (rdr.Read())
            {
                Data1 data = new Data1();
                data.CustomerId = Convert.ToInt32(rdr["CustomerId"]);
                data.CustomerName = rdr["CustomerName"].ToString();
                data.Engineer = rdr["Engineer"].ToString();
                Data.Add(data);
            }

        }
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Serialize(Data);
    }

任何帮助将不胜感激,需要更多信息让我知道。

1 个答案:

答案 0 :(得分:0)

在您的Web服务中,您已经设置了$post_type_list = include_post_types(); $sitemap_post_args = array( 'post_type' => $post_type_list, 'posts_per_page' => -1, 'orderby' =>'post_type', 'order' =>'asc' ); ,这意味着可以使用Http GET请求而非POST执行该方法

UseHttpGet=true

但是,在javascript中,您尝试使用客户端的POST执行方法

 [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
 public string GetData(bool withfilters, string filteredtext)

解决方案:

从ScriptMethod属性中删除HttpGet = true:

 $.ajax({
            url: '/WebService.asmx/GetData',
            method: 'post',
            dataType: 'json',