我整天都在研究这个问题,这似乎是一个常见的问题,但是我找不到解决方案。
我正在使用jquery的$.ajax()
函数进行服务调用,以更新数据库中的某些值。它在localhost上运行良好,但是在实际服务器上,控制台窗口中出现500 Internal Server Error。
我的客户端代码如下:
var param = FunctionToMakeKeyValueString();
$.ajax({
type: "POST",
url: "../Helpers/Autocomplete.asmx/OrderStatements",
data: { param: param },
clientType: "application/json; charset=utf-8",
datatype: "json",
async: true,
success: function () { ShowSuccess();},
error: function () { ShowError(); }
});
服务器端代码是:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public void OrderStatements(string param)
{
IncomeStatementService service = new IncomeStatementService();
string[] comps = param.Split(';');
foreach (string s in comps)
{
int id;
short order;
string[] pieces = s.Split(':');
if (int.TryParse(pieces[0], out id) && short.TryParse(pieces[1], out order))
{
IncomeStatement statement = service.FindBy(id);
statement.Order = order;
service.UpdateOrder(statement);
}
}
}
}
实际的asmx文件只是
<%@ WebService Language="C#" CodeBehind="~/App_Code/AutoComplete.cs" Class="AutoComplete" %>
我确定URL是正确的(.js文件位于Helpers的同级文件夹中,该文件夹包含asmx),但是还需要在IIS或web.config文件中设置其他内容吗?谢谢!
@Mike W的评论使我对服务器错误日志进行了一些调查,结果发现:
Exception type: InvalidOperationException
Exception message: Request format is unrecognized for URL unexpectedly ending in '/OrderStatements'.
我用谷歌搜索了此消息,这使我想到了这个堆栈溢出问题,似乎就像将其添加到配置文件的systen.web部分中一样简单:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句