【发布时间】:2012-11-20 10:00:41
【问题描述】:
我正在将 ajax 连接到 wcf 服务。但是继续获取方法是不允许的。调试了几天。我不明白。 我只是在测试默认的 GetData(int value) 方法。
阿贾克斯:
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "http://localhost:19478/Service1.svc/GetData",
data: JSON.stringify({"value": "test"}),
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function (msg) {
alert(msg);
},
error: function (msg) {
alert("Failed");
}
});
function OnSuccessCall(response) {
alert(response);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
</script>
web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceTest.Service1" behaviorConfiguration="myServiceBehavior">
<endpoint name="webHttpBinding"
address="" binding="webHttpBinding"
contract="WcfServiceTest.IService1"
behaviorConfiguration="webHttp"
>
</endpoint>
<endpoint name="mexHttpBinding"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
<behavior name="NewBehavior0">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Iservice1:
[OperationContract]
[WebInvoke(Method="POST",
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
string GetData(String value);
服务1:
public string GetData(String value)
{
return string.Format("You entered: {0}", value);
}
public class Service1 : IService1 上面什么都没有。
在public interface IService1上方有一个[ServiceContract]。
我添加了很多东西,删除了很多东西..我不知道了。 我怀疑它是我的 web.config 文件,我不明白那部分
【问题讨论】:
-
尝试指定
UriTemplate,它应该类似于WebInvoke(Method="POST", UriTemplate="GetData")。另外,jsonp和json不一样,所以你应该在你的ajax 调用中使用dataType: "json" -
尝试了第一件事。 Json 的问题在于它给了我不允许的错误方法和
Origin null is not allowed by Access-Control-Allow-Origin。我刚刚做的是将我的 ajax 文件移动到我的 wcf 项目的根目录。我收到此错误:'There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'value' from namespace -
最后一个错误的原因是参数的类型不匹配。我不确定如何解决,但您可以尝试删除或更改合同中的 RequestFormat=WebMessageFormat.Json。
标签: c# jquery ajax wcf web-config