簡體   English   中英

從jquery .ajax()使用時,啟用Ajax的WCF服務(JSON)出錯

[英]Error with Ajax-Enabled WCF Service (JSON) when consuming from jquery .ajax()

不幸的是,只有在調用.ajax()和textStatus(第二個參數)時才會出現錯誤情況。 我已經閱讀了關於stackoverflow的幾個例子和其他問題,但必須遺漏一些東西。 在此先感謝您的幫助。

WCF服務:

[ServiceContract(Namespace = "http://localhost/")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Items
{
    [OperationContract]
    [WebGet]
    public string HelloWorld()
    {
        return "Hello World.";
    }
}

WCF Web.config

僅相關部件...服務:

<services>
  <service name="OService.ReadServices.Items">
    <endpoint address="soap" binding="basicHttpBinding" contract="OService.ReadServices.Items"/>
    <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="OService.ReadServices.Items"/>
  </service>
</services>

行為:

<endpointBehaviors>
    <behavior name="jsonBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>

jQuery的

$(document).ready(function () {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost/OService/ReadServices/Items.svc/json/HelloWorld",
        data: "{}",
        dataType: "json",
        success: function (msg) {
            alert("success: " + msg.d);
        },
        error: function (xhr, textStatus, errorThrown) {
            alert("error: " + textStatus + " - " + errorThrown + " - " + xhr);
        }
    });
});

這個回復有點遲了,但您將Web方法定義為[WebGet]但在Jquery Ajax方法中將其稱為POST請求。 [WebGet]替換為:

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM