我已经尝试了几乎所有我能想到的东西,但我仍然遇到我对 WCF 服务的 ajax 调用的问题。
我的 WCF 服务有如下方法:
//[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "POST")]
[WebGet]
public string Test(int value)
{
return string.Format("You entered: {0}", value);
}
正如Patrick Thomas在 Twitter 上提到的,我也尝试过使用[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped)]
,[WebGet(BodyStyle = WebMessageBodyStyle.WrappedResponse)]
但没有成功。
和这样的配置:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RestEndpoint">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="NoSecurityRestBinding" crossDomainScriptAccessEnabled="true">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="WebServices.TestService">
<endpoint address="" binding="webHttpBinding" contract="WebServices.ITestService" bindingConfiguration="NoSecurityRestBinding" behaviorConfiguration="RestEndpoint" />
</service>
</services>
</system.serviceModel>
该服务位于不同的域中,因此我将数据格式化为 JSONP:
$.ajax({
cache: false,
async: true,
type: "GET", // Was "POST"
dataType: "jsonp",
url: "http://mydomain.com/TestService.svc/Test?callback=?",
data: dataToPost,
contentType: "application/json;charset=utf-8",
success: function (msg) {
var testMsg = JSON.parse(msg);
var status = testMsg.TestResult;
alert(status);
},
error: function (msg) {
alert('Please email Jason with this exception: ' + msg.statusText);
}
});
我得到:
“解析器错误”
“没有调用 jQuery16408722478272714725_1332817261195”
我可能做错了什么?我确实验证了所有 WCF 二进制文件都是 4.0。
在此先感谢您的帮助!