我正在将 ajax 连接到 wcf 服务。但是不允许继续获取方法。调试了好几天。我不明白。 我只是在测试默认的 GetData(int value) 方法。
Ajax :
<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>
网络配置:
<?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>
I服务1:
[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 文件,我不明白那部分
最佳答案
您的操作契约(Contract)表明它是一个 post 方法,但您请求它作为一个仅支持 Get
请求的 JSONP
。如果它不是跨域请求,您不需要使用 JSONP 只需将方法设置为 Post
并删除类型,您的响应格式也不是 JSON 对象,也可以更改它,因此您的需要,通过更改契约(Contract)或更改方法中的返回数据,然后它应该可以工作。
编辑评论:
首先,JSONP 不是实际的 xmlhttprequest 对象请求。它正在做的是将一个脚本标记添加到您的页面,该页面具有您的回调函数,请求数据作为参数。它主要针对跨域数据共享。 JSONP 请求返回如下内容
请求网址:domain.com/getJsonp?callback=processJSONP
哪个返回;
processJSONP( {
resultList: [{data: "hello"},
{data: "world"}
// and lost of data you need.
]
});
请注意 processJSONP,这是您在页面或库中的功能,您可以随心所欲。
function processJSONP(jsonpResult) {
for(var key in jsonpResult.resultList)
{
//process the data
}
}
如果您确实需要使用 POST 获取数据,那么它不能是 JSONP。它必须是一个 AJAX 请求并且也必须在同一个域中。在这种情况下,您可以在 AJAX 请求的成功函数中处理数据。
关于c# - WCF 和 Ajax。不允许的方法让我发疯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13470714/