繁体   English   中英

从 Ajax 调用 .Net 中的 SOAP Web 方法获取 500 服务器错误

[英]Getting 500 server error from Ajax call to a SOAP web method in .Net

我正在使用 ajax 进行两个不同的异步调用。 两者都以相同的方式完成,但其中一个具有 Web 方法的参数。 该链接是正确的,当我关注它时,我会在浏览器中正确返回,但我只是不断收到服务器 500 错误。 有任何想法吗? 一个没有参数就可以正常工作,这里是 JS/jQuery 代码,因为我有点确定它不在 C# 方面:

function CatChanged() {
    var strA = $('#ddlCategory').val();
    $.ajax({
        url: encodeURI('https://localhost:44380/WebService1.asmx/GetProducts?category=' + strA),
        data: '<soap: Envelope xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd="http://www.w3.org/2001/XMLSchema" xmlns: soap="http://schemas.xmlsoap.org/soap/envelope/%22%3E<soap: Body><xmlns="http://tempuri.org/" /></soap: Body></soap: Envelope >',
        type: 'POST',
        contentType: "text/xml",
        dataType: "text/xml",
        success: function (response) {
           alert(response.responseText);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("ERROR!!");
            alert(xhr.status);
            alert(thrownError);
        }
    });
}

下面是来自 C# 代码的 web 方法代码:

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetCategories()
        {
            Catalog cat = new Catalog();
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(cat.categories);
        }

        [WebMethod]
        public string GetProducts(string category)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Catalog cat = new Catalog();
            List<string> retList = new List<string>();
            switch (category)
            {
                case "Electronics":
                    foreach (Product product in cat.electronicProducts)
                    {
                        retList.Add(product.itemName);
                    }
                    return jss.Serialize(retList);
                case "Apparel":
                    foreach (Product product in cat.apparelProducts)
                    {
                        retList.Add(product.itemName);
                    }
                    return jss.Serialize(retList);
                case "Food and Drink":
                    foreach (Product product in cat.electronicProducts)
                    {
                        retList.Add(product.itemName);
                    }
                    return jss.Serialize(retList);
                default:
                    foreach (Product product in cat.electronicProducts)
                    {
                        retList.Add(product.itemName);
                    }
                    return jss.Serialize(retList);
            }
        }

...这是我在 Chrome 中按照开发人员控制台中的直接链接在浏览器中获得的返回的屏幕截图: 在此处输入图片说明

当从 url 调用时,web 方法的返回都很好。 这让我认为它必须是 ajax 调用中的某些内容,并且我不确定当我将参数添加到 Web 方法时需要做什么不同的事情才能使用它。 就像我说的,第一个 web 方法在我的 JavaScript 代码中工作,我能够很好地解析返回的所有内容。

此外,使用任何其他服务、MVC 或任何类似的东西都不是一种选择。 鉴于目前的情况,我必须让它发挥作用。

好吧,如果我理解正确的话。 当您使用浏览器访问 URL 时,结果工作正常。 但是,当您执行 ajax 调用时,您会遇到麻烦。

我从您的代码中注意到的一件事是,您正在使用 ajax 调用执行 POST 请求,但方法都是 GET。 尝试将 ajax 调用中的类型更改为 GET。

例如

function CatChanged() {
    var strA = $('#ddlCategory').val();
    $.ajax({
        url: encodeURI('https://localhost:44380/WebService1.asmx/GetProducts?category=' + strA),
        data: '<soap: Envelope xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd="http://www.w3.org/2001/XMLSchema" xmlns: soap="http://schemas.xmlsoap.org/soap/envelope/%22%3E<soap: Body><xmlns="http://tempuri.org/" /></soap: Body></soap: Envelope >',
        type: 'GET', // <-- Changed from POST to GET
        contentType: "text/xml",
        dataType: "text/xml",
        success: function (response) {
           alert(response.responseText);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("ERROR!!");
            alert(xhr.status);
            alert(thrownError);
        }
    });
}

暂无
暂无

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

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