jQuery Ajax 调用适用于本地主机,但不适用于实时服务器
本文关键字:适用于 不适用 实时 服务器 Ajax 调用 jQuery 主机 | 更新日期: 2023-09-27 18:36:59
我研究了一整天这个问题,这似乎是一个有点普遍的问题,但我无法找到解决方案。
我正在使用jquery的$.ajax()
函数进行服务调用,以更新数据库中的某些值。 它在本地主机上运行良好,但在实际服务器上,我在控制台窗口中收到 500 内部服务器错误。
我的客户端代码如下:
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文件位于包含 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>
你能尝试将以下行添加到你的web.config httpHandlers中吗?
<system.web>
<httpHandlers>
<add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services" />
</httpHandlers>
</system.web>
您在 JavaScript/Jquery 中提供的主机链接可能存在问题
请记下 如果您在chrome上查看此内容,请清除浏览器数据,您将在两端看到相同的结果