问题描述
我尝试使用 AJAX 从 ASPX 页面调用 WCF 服务.
I try to call WCF service from ASPX page with AJAX.
我收到错误消息:传入的消息具有意外的消息格式 'Raw'.该操作的预期消息格式是 'Xml'; 'Json'.这可能是因为尚未在绑定上配置 WebContentTypeMapper. 有关详细信息,请参阅 WebContentTypeMapper 的文档."
I get the error: "The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml'; 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details."
代码如下.(不带参数请求调用成功!)
Code is below.(Without parameters request calling successfully!)
服务方式:
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
string SimpleRequest(string fullname);
Web.config:
Web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="GenesysService.GenesysService">
<endpoint address=""
binding="webHttpBinding"
contract="GenesysService.IGenesysService"
behaviorConfiguration="ServiceAspNetAjaxBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<serviceHostingEnvironment
multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true" />
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Methods" value="GET, POST"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>`
AJAX 请求:
$.ajax({
type: "POST",
url: 'http://localhost:56428/GenesysService.svc/SimpleRequest',
data: JSON.stringify({fullname: "fullname"}),
dataType: "json",
crossDomain: true,
success: function (response) {
alert('success!');
alert(response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error!');
alert(xhr.responseText);
}
});
我发现了同样的错误一>.但后来我添加了下一个字符串:
I found the same mistake. But then I add the next string:
contentType: "application/json"
我收到错误消息:OPTIONS 405(不允许的方法)XMLHttpRequest 无法加载 .无效的 HTTP 状态码 405 "
I get error: "OPTIONS <...> 405 (Method Not Allowed)XMLHttpRequest cannot load <...>. Invalid HTTP status code 405 "
推荐答案
Ajax 调用中的 Method Not Allowed 响应是由于 Javascript 单源策略:
The Method Not Allowed response in your Ajax call is due to the Javascript single origin policy:
http://en.wikipedia.org/wiki/Same-origin_policy
要从您的页面调用服务,它需要使用相同的端口和协议,或者您可以使用 JSONP.
To call the service from your page it needs to be on the same port and protocol, or you can use JSONP.
更多信息在这里:http://www.sitepoint.com/jsonp-examples/
这篇关于WCF 请求:&quot;传入的消息具有意外的消息格式 &#39;Raw&#39;.该操作的预期消息格式为 &#39;Xml&#39;;&#39;Json&#39;&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!