簡體   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