[英]HTTP 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
[英]Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
通過 ajax json 調用 WCF 服務時,我得到了上述響應。 我的調用代碼是:
<script type="text/javascript">
$(document).ready(function () {
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:90/WebServices/UserService.svc/Calculate",
data: "{}",
timeout: 10000,
dataType: "json",
success: function (response) {
alert(response)
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(thrownError);
}
});
});
</script>
我的服務是:
[ServiceContract]
public interface IUserService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json
)]
Answer Calculate();
}
[DataContract]
public class Answer
{
[DataMember]
public string answer { get; set; }
}
我的方法是:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class UserService : IUserService
{
public Answer Calculate()
{
Answer answer = new Answer();
answer.answer="Hello World";
return answer;
}
}
我一直在與之斗爭一段時間,我看到其他人也遇到過同樣類型的問題,我嘗試了所有的建議,但仍然沒有任何效果。
問題出在哪里? 我該如何解決?
我猜這里是因為你沒有展示你是如何定義你的端點的,但我很確定是這樣的。 您的端點不是為網絡消費定義的 - 它可能使用basicHttpBinding
。 要通過 jQuery(或一般的其他 web/REST 客戶端)使用端點,您需要使用WebHttpBinding
定義一個端點,並將WebHttpBehavior
應用於它。
有多種方法可以正確定義端點。 最簡單的方法是在 .svc 文件中使用WebServiceHostFactory
:
用戶服務.svc :
<%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.UserService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
對於通過搜索到達這里的任何人:'content type'application/json; charset=utf-8' 不是預期的類型 'text/xml; 字符集=utf-8'
在我的案例中,由於構建和運行沒有適當屬性的服務而導致了類似的錯誤。 當我嘗試在客戶端應用程序中更新服務引用時收到此錯誤消息。 當我將“[DataContract]”和“[DataMember]”屬性正確應用到我的自定義類時,它得到了解決。
編輯:如果您的服務已設置並正常工作,然后在您編輯后它中斷了,這很可能適用。
這是一個很老的問題,但我想在@carlosfigueira 的回答中添加一些信息。
1. 在.svc
中指定Factory
與在web.config
中指定<service>
在.svc
文件中指定Factory
的替代方法是在web.config
文件中指定<service>
。 這在許多其他答案(例如這個)中都有解釋,所以我不會重復它們,但值得在這里復制另一個@carlosfigueira答案:
如果您未在 .svc 文件中指定任何工廠,則所有端點都將來自 web.config 文件 - WCF 將嘗試查找
name
屬性與完全限定名稱匹配的<system.serviceModel / service>
元素服務等級。 如果找不到,那么它將添加一個默認端點(使用basicHttpBinding
,除非您更改了默認映射)。
並且要清楚:完全限定的名稱應該包括命名空間。 因此,如果有以下內容,那將是MyWebServices.ExampleWebService
:
namespace MyWebServices
{
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ExampleWebService
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public string DoWork(DoWorkRequest request)
{
return "success!";
}
}
}
2. 在 localhost 上調用時出現“無法處理消息...”
當我嘗試從 Visual Studio 在 localhost 上運行 ajax 調用時,我遇到了這個問題,並得到了這個問題的標題錯誤消息,所以我想列出解決它所需的步驟:
.svc
中添加Factory
,要么在web.config
中指定需要的內容。.html
文件,然后選擇“在瀏覽器中查看”。 您的默認瀏覽器將打開,如果是 Chrome,則 URL 將是,例如localhost:51667/test.html
(實際上是http://localhost:51667/test.html
)。就我而言,問題是請求方法。 我試圖發送 GET 請求而不是 POST 請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.