[英]No ajax call on WCF service with method
I am sending a simple ajax call with WCF method, whenit is returning bad-request
but without method it is showing status to success 我正在使用WCF方法发送一个简单的Ajax调用,当它返回
bad-request
但没有方法时,它将显示成功状态
Ajax Call 阿贾克斯电话
$(document).ready(function(){
$.get("http://localhost:1347/Service1.svc/DoWork", //this shows bad-request
//$.get("http://localhost:1347/Service1.svc", //this shows success
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
Operation Contract 经营合同
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string DoWork();
}
}
Implementation 实作
public class Service1 : IService1
{
public string DoWork()
{
return string.Format("This is DoWork()");
}
}
Global.asax Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
Web.Config (Edit) Web.Config(编辑)
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
preCondition="managedHandler"/>
</modules>
<!--
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"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>
First, make sure that you enable GET in your contract. 首先,请确保在合同中启用GET。
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "DoWork", ResponseFormat = WebMessageFormat.Json)]
string DoWork();
}
}
And then make sure to add webHttp
in your config. 然后确保在配置中添加
webHttp
。
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
You also might need to set your binding to webHttpBindig
. 您可能还需要将绑定设置为
webHttpBindig
。
I usually define my endpoints, but I think you can manage without it. 我通常定义端点,但是我认为没有端点也可以管理。 This should be a complete example.
这应该是一个完整的例子。
<system.serviceModel>
<services>
<service name="WcfService1.IService1" behaviorConfiguration="myServiceBehavior">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="myWebBehavior" contract="WcfService1.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="myWebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="myServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
A complete example can be found here. 一个完整的例子可以在这里找到。
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.