问题
I have an web service named AEWService.asmx with the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace editor
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class AEWService : System.Web.Services.WebService
{
[WebMethod]
public static Dictionary<string, string> TestFunc(string elementToBeModified, string selectedValue)
{
Dictionary<string, string> newValues = new Dictionary<string, string>();
newValues.Add("k1", "val1");
newValues.Add("k2", "val2");
return newValues;
}
}
}
When I call the webmethod with ajax, I get a 500 error - Unknown web method TestFunc. The ajax call looks like this:
var dataString = JSON.stringify({
"elementToBeModified": "someElement",
"selectedValue": "someValue"
});
$.ajax({
url: 'AEWService.asmx/TestFunc',
type: "POST",
data: dataString,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// do somethig...
}
});
I also added the following lines into web.config under tags (I'm not sure what they mean but I hope they are ok):
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
The code worked perfect when the webmethod was on a aspx page. What's wrong with the asmx?
Thanks!
回答1:
You can not return a dictionary object from webservice. See this post. http://forums.asp.net/t/1370858.aspx/1
Also don't mark the webmethod as static. Check here Why are Static Methods not Usable as Web Service Operations in ASMX Web Services?
来源:https://stackoverflow.com/questions/8489707/asp-net-calling-webmethods-inside-an-webservice-internal-server-error