首页 文章

使用AJAX调用web方法时出现内部服务器错误(500)[暂停]

提问于
浏览
0

嗨,我在AJAX中调用web方法时遇到外部服务器错误 .

HERE IS THE AJAX CODE

$(document).ready(function () {
        $(document).ajaxError(function (xhr) {
            alert(xhr.status);
        });
        $.ajax({
            url: '/itmWS.asmx/GetItems',
            method: 'POST',
            dataType: 'json',
            error: function (xhr, textStatus, err) {
               alert("readyState: " + xhr.readyState);
               alert("responseText: " + xhr.responseText);
                alert("status: " + xhr.status);
               alert("text status: " + textStatus);
                alert("error: " + err);
            },
            success: function (data) {
                $('#datatable').DataTable({
                    data: data,
                    columns: [
                        { 'data': 'Name' },
                        { 'data': 'Brand' },
                        { 'data': 'ReplenishLimit', "width": "110px" },
                        { 'data': 'ReplenishQuantity', "width": "100px" },
                        { 'data': 'Quantity', "width": "70px" },
                    ],
                    'columnDefs': [{
                        'targets': 5,
                        'searchable': false,
                        'orderable': false,
                        "data": "Id",
                        'render': function (data, type, full, meta) {
                            return '<input type="checkbox" name="checkBox" value="'
                                + $('<div/>').text(data).html() + '">';
                        }
                    }],
                });
            }

        });
    });

HERE IS THE WEB METHOD

public class itmWS : System.Web.Services.WebService
{
    SqlConnection con = new 
    SqlConnection(COMMON.CommonFunctions.getconstring());
    [WebMethod]
    public void GetItems()
    {
        List<Items> itm = new List<Items>();
        using (con)
        {
            SqlCommand cmd = new SqlCommand("GET_ITEMS", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Items item = new Items();
                item.Id = dr["id"].ToString();
                item.Name = dr["Name"].ToString();
                item.Brand = dr["Brand"].ToString();
                item.ReplenishLimit = dr["ReplenishLimit"].ToString();
                item.ReplenishQuantity = dr["ReplenishQuantity"].ToString();
                item.Quantity = dr["Quantity"].ToString();
                itm.Add(item);
            }
        }
        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(itm));
    }
}

它适用于localhost / development,但不适用于 生产环境 生产环境 服务器 . Web方法上的代码很好,我怀疑URL导致错误 . 请帮忙

2 回答

  • 0

    我能够找到具体的问题而且确实如此

    Request format is unrecognized for URL unexpectedly ending in /myMethodName

    解决方案是在Web配置上添加ff行

    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
    
  • 0

    尝试从Ajax url中删除“/”:'/ itmWS.asmx / GetItems',

    我认为它应该像url:'itmWS.asmx / GetItems',

相关问题