jquery ajax调用适用于localhost,但不适用于实时服务器
我整天都在研究这个问题,这似乎是一个常见的问题,但我找不到解决方案。
I've researched this issue all day and it seems to be a somewhat common problem, but I have not been able to find a solution.
我正在使用jquery的 $。ajax()
用于进行更新数据库中某些值的服务调用的函数。它在localhost上运行正常,但在实际服务器上,我在控制台窗口中收到500内部服务器错误。
I am using jquery's $.ajax()
function to make a service call that updates some values in the database. It runs fine on localhost, but on the actual server I get a 500 Internal Server Error in the console window.
我的客户端代码如下:
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文件只是
the actual asmx file is just
<%@ WebService Language="C#" CodeBehind="~/App_Code/AutoComplete.cs" Class="AutoComplete" %>
我确定网址是正确的(.js文件位于帮助程序的兄弟文件夹中,其中包含asmx),但我还需要在IIS或web.config文件中设置其他内容吗?谢谢!
I'm sure the url is correct (the .js file is in a sibling folder to Helpers, which contains the asmx), but is there something else that I need to set in IIS or the web.config file? Thanks!
@Mike W评论让我看了一下服务器错误日志,我找到了:
Comment by @Mike W led me to look into the server error logs a bit, where I found:
Exception type: InvalidOperationException
Exception message: Request format is unrecognized for URL unexpectedly ending in '/OrderStatements'.
我用Google搜索了一条消息,让我这个堆栈溢出问题
看起来好像很简单将其添加到配置文件的systen.web部分:
I googled that message which let me to this stack overflow question and it seems like it was as simple as adding this to the systen.web section of my config file:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>