javascript - 从 Ajax 调用 .Net 中的 SOAP Web 方法时出现 500 服务器错误

标签 javascript c# .net ajax soap

我正在使用 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 中点击开发者控制台的直接链接时在浏览器中获得的返回结果的屏幕截图: enter image description here

当从 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);
        }
    });
}

关于javascript - 从 Ajax 调用 .Net 中的 SOAP Web 方法时出现 500 服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59957938/

相关文章:

javascript - QUnit - equal 与assert.equal?

c# - 编译器错误说我没有分配局部变量并且名称在当前上下文中不存在,而它确实存在?

c# - 我可以从 C# 中同一类的另一个构造函数调用重载的构造函数吗?

.net - WatiN 停产了吗?

.net - Excel 从两个不同的 AppDomain 调用 .NET 自动化服务器?

javascript - 在 Vue.js 中,为什么我的计算属性不起作用

javascript - 如何在 JavaScript 中保存一组特定的图像类别?

javascript - 防止默认不起作用

c# - 使用 Entity Framework 构建一个简单的多线程时事通讯引擎

c# - 如何让不同的解决方案引用一个 resx 文件?