简体   繁体   English

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

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

I have two different async calls I am doing with ajax.我正在使用 ajax 进行两个不同的异步调用。 Both are done the same way, but the one has parameters for the web method.两者都以相同的方式完成,但其中一个具有 Web 方法的参数。 The link is correct, when I follow it I get the return correctly in the browser, but I just keep getting a server 500 error.该链接是正确的,当我关注它时,我会在浏览器中正确返回,但我只是不断收到服务器 500 错误。 Any ideas?有任何想法吗? The one works fine without parameters, here is the JS/jQuery code since I am slightly sure it isn't on the C# side:一个没有参数就可以正常工作,这里是 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);
        }
    });
}

Here is the web method code from the C# code behind:下面是来自 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);
            }
        }

... Here is a screenshot of the return I get in the browser when I follow the direct link from the developer's console in Chrome: ...这是我在 Chrome 中按照开发人员控制台中的直接链接在浏览器中获得的返回的屏幕截图: 在此处输入图片说明

The return is all good from the web method when called from the url.当从 url 调用时,web 方法的返回都很好。 In which leads me to think that it has to be something in the ajax call, and i'm not sure what I need to do different in order to consume it when I add the parameter to the web method.这让我认为它必须是 ajax 调用中的某些内容,并且我不确定当我将参数添加到 Web 方法时需要做什么不同的事情才能使用它。 Like I said, the first web method works in my JavaScript code, and I was able to parse everything from the return just fine.就像我说的,第一个 web 方法在我的 JavaScript 代码中工作,我能够很好地解析返回的所有内容。

Also, using any other services, MVC, or anything like that isn't an option.此外,使用任何其他服务、MVC 或任何类似的东西都不是一种选择。 I have to make it work given the current situation.鉴于目前的情况,我必须让它发挥作用。

Alright so if i understand you correctly.好吧,如果我理解正确的话。 When you visit the URL with your browser the results work just fine.当您使用浏览器访问 URL 时,结果工作正常。 But when you do an ajax call you're having troubles.但是,当您执行 ajax 调用时,您会遇到麻烦。

The one thing i noticed from your code is that you're doing a POST request with your ajax call but the methodes are both GET.我从您的代码中注意到的一件事是,您正在使用 ajax 调用执行 POST 请求,但方法都是 GET。 Try changing the type within your ajax call to GET.尝试将 ajax 调用中的类型更改为 GET。

Eg例如

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.

相关问题 Laravel - Ajax 调用上的 500(内部服务器错误) - Laravel - 500 (Internal Server Error) on Ajax Call Ajax调用返回500内部服务器错误 - Ajax call returning 500 internal server error 使用 ajax(发送方法)在服务器上出现错误 500 - Error 500 on server with ajax (send method) 在C#中调用Web服务器方法时,ajax中的500 Internal Server Error - 500 Internal Server Error in ajax when calling web server method in C# jQuery Ajax和服务器端方法调用错误asp.net - Jquery ajax and server side method call error asp.net 尝试对 Web 表单方法进行 Ajax 调用并收到内部服务器错误。 我已经尝试了几乎所有类似问题的解决方案 - Trying to make an Ajax call to a web form method and getting an internal server error. I have tried almost all solutions to similar questions ASP.NET MVC 500(内部服务器错误)与 ajax post 方法 - ASP.NET MVC 500 (Internal Server Error) with ajax post method 调用服务器时,ajax 调用中出现错误 500,但在 localhost 中有效 - error 500 in ajax call when call to the server, but in localhost works 对Webmethod的Ajax调用给出错误500内部服务器错误 - Ajax call to webmethod gives ERROR 500 Internal server error 尝试在Visual Studio中从JavaScript调用插入值到SQL Server时出现内部服务器错误500 - Getting a internal server error 500 while trying to call insert values from javascript into sql server in visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM