jquery ajax调用适用于localhost,但不适用于实时服务器

时间:2013-10-25 23:33:19

标签: c# javascript ajax jquery

我整天都在研究这个问题,这似乎是一个常见的问题,但我找不到解决方案。

我正在使用jquery的$.ajax()函数进行服务调用,以更新数据库中的某些值。它在localhost上运行正常,但在实际的服务器上,我在控制台窗口中收到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" %>

我确定网址是正确的(.js文件位于包含asmx的Helpers的兄弟文件夹中),但是我还需要在IIS或web.config文件中设置其他内容吗?谢谢!

3 个答案:

答案 0 :(得分:2)

@Mike W的评论让我看了一下服务器错误日志,我找到了:

Exception type: InvalidOperationException 
Exception message: Request format is unrecognized for URL unexpectedly ending in '/OrderStatements'.

我用Google搜索了那条让我this stack overflow question的消息 它似乎就像将它添加到我的配置文件的systen.web部分一样简单:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>

答案 1 :(得分:1)

您可以尝试将以下行添加到您的web.config httpHandlers

<system.web>
  <httpHandlers>
     <add verb="*" path="*.asmx" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services" />
  </httpHandlers>
</system.web>

答案 2 :(得分:0)

您在JavaScript / Jquery

中提供的主机链接可能存在问题

请记下来如果您正在查看Chrome,请清除浏览器数据,您将在两端看到相同的结果