繁体   English   中英

jQuery ajax调用在本地主机上有效,但在实时服务器上无效

[英]jquery ajax call works on localhost, but not on live server

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

我正在使用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>

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

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

您在JavaScript / Jquery中提供的Host链接可能存在问题

请记下笔记如果您在chrome上查看此内容,请清除浏览器数据,并且两端都会看到相同的结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM