簡體   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