Having trouble with two different async calls using ajax. Both are done the same way, but one has parameters for the web method. The link is correct, as I get the return correctly in the browser when I follow it, but I keep getting a server 500 error. Any ideas? The call works fine without parameters. Here's the JS/jQuery code:
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's the web method code from the C# code behind:
[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);
}
}
... Screenshot of the return in the browser when following the direct link from the developer's console in Chrome: https://i.sstatic.net/gFmYb.png
The return is good from the web method when called from the URL, indicating an issue with the ajax call. Need to figure out what needs to be changed to consume the parameter in the web method. First web method works, able to parse everything from the return just fine.
Using any other services, MVC, or similar options isn't possible. Need to make it work given the current limitations.