WCF POST Call不起作用 [英] WCF POST CALL IS NOT WORKING
问题描述
我已经尝试过多次调用wcf服务,但是它不起作用,但是get呼叫工作正常.这里的代码:-
WCF IService->
Hi,
I have lot tried post call to wcf service but its not working,but get call is working fine.here''s code:-
WCF IService->
[ServiceContract]
public interface IService
{
[OperationContract]
void DoWork();
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/TestMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string TestMethod();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/TestMethod_FORPOST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string TestMethod_FORPOST(string param1);
}
Wcf-Service.svc.cs:-
Wcf-Service.svc.cs:-
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service : IService
{
public void DoWork()
{
}
public string TestMethod()
{
return "Success";
}
public string TestMethod_FORPOST(string param1)
{
return "SuccessFullyPosted";
}
Web Confi:-<!-CustomSettingsForWCF->
Web Confi:- <!--CustomSettingsForWCF-->
<system.servicemodel>
<bindings>
<webhttpbinding>
<binding name="webBinding" maxreceivedmessagesize="2147483647" crossdomainscriptaccessenabled="true">
maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
transferMode="Streamed">
<readerquotas maxstringcontentlength="2147483647" maxarraylength="2147483647" />
<!--<security mode="Transport">
<transport>
<extendedprotectionpolicy policyenforcement="Never" />
</transport>
</security>-->
</binding>
</webhttpbinding>
</bindings>
<services>
<service behaviorconfiguration="Service" name="WCF_TEST_APPLICATION_WITH_AJAXCALL.Service">
<endpoint address="" behaviorconfiguration="webEndpoint">
binding="webHttpBinding" bindingConfiguration="webBinding" name="web"
bindingNamespace="" contract="WCF_TEST_APPLICATION_WITH_AJAXCALL.IService" />
</endpoint></service>
</services>
<behaviors>
<endpointbehaviors>
<behavior name="webEndpoint">
<webhttp helpenabled="true" defaultbodystyle="Wrapped" defaultoutgoingresponseformat="Json" />
</behavior>
</endpointbehaviors>
<servicebehaviors>
<behavior name="Service">
<servicemetadata httpgetenabled="true" />
<servicedebug includeexceptiondetailinfaults="false" />
<!--<userequestheadersformetadataaddress />-->
</behavior>
</servicebehaviors>
</behaviors>
<servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true" />
</system.servicemodel>
<system.webserver>
<modules runallmanagedmodulesforallrequests="true" />
</system.webserver>
整个Call方法中的另一个应用程序:-
Another application throughout Call method:-
var WCFurl = "http://localhost:49212/Service.svc/TestMethod_FORPOST";
var datar = { param1: "abc" };
alert("SecondCall");
$.ajax({
type: "POST",
//contentType: "application/json; charset=utf-8",
url: WCFurl,
data: JSON.stringify(datar),
dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
},
success: function (msg) {
alert("success");
},
error: function (xhr, status, error) {
alert(xhr.status + " " + status + " " + error);
},
complete: function (data) {
alert(JSON.stringify(data));
}
});
上面的代码可以很好地用于接听电话,但不适用于接听电话,请给我建议,如何处理接听电话?
-Sanjiv
above code is working fine for get call but not for post call, please give me suggestion, how can i do with post call?
-Sanjiv
推荐答案
.ajax({ 类型:" , // contentType:"application/json; charset = utf-8", 网址:WCFurl, 数据: JSON .stringify(datar), dataType:" , beforeSend:功能(xhr){ xhr.setRequestHeader(" , *"); }, 成功:功能(msg){ alert(" ); }, 错误:功能(xhr,状态,错误){ alert(xhr.status + " +状态+ " " +错误); }, 完成:功能(数据){ alert( JSON .stringify(data)); } });
.ajax({ type: "POST", //contentType: "application/json; charset=utf-8", url: WCFurl, data: JSON.stringify(datar), dataType: "json", beforeSend: function (xhr) { xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); }, success: function (msg) { alert("success"); }, error: function (xhr, status, error) { alert(xhr.status + " " + status + " " + error); }, complete: function (data) { alert(JSON.stringify(data)); } });
上面的代码可以很好地用于接听电话,但不适用于接听电话,请给我建议,如何处理接听电话?
-Sanjiv
above code is working fine for get call but not for post call, please give me suggestion, how can i do with post call?
-Sanjiv
这篇关于WCF POST Call不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!