我在配置我的 WCF 服务以允许通过跨域 AJAX 消费时遇到了一点困难。
我已经成功地在客户端上使用了这项服务,没有任何问题,但是每次我尝试使用 AJAX(通过 jQuery 中的 $.ajax 对象)点击它时,都会收到 400 错误。
这是我的 web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding" />
</basicHttpBinding>
<mexHttpBinding>
<binding name="mexHttpBinding" />
</mexHttpBinding>
<webHttpBinding>
<binding name="webHttpBinding" crossDomainScriptAccessEnabled="true">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="Services.WebAnalyticsService">
<clear />
<endpoint binding="basicHttpBinding" bindingConfiguration="basicHttpBinding"
name="WebAnalyticsDotNetClientEndpoint" contract="Contracts.IWebAnalyticsService"
listenUriMode="Explicit" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="mexHttpBinding"
name="WebAnalyticsMetaDataEndpoint" contract="Contracts.IWebAnalyticsService"
listenUriMode="Explicit" />
<endpoint address="script" behaviorConfiguration="aspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webHttpBinding"
name="WebAnalyticsAjaxEndpoint" contract="Contracts.IWebAnalyticsService" />
<!--<endpoint address="web" behaviorConfiguration="RESTBehavior"
binding="webHttpBinding" bindingConfiguration="webHttpBinding"
name="WebAnalyticsAjaxEndpoint" contract="Contracts.IWebAnalyticsServiceWeb" />-->
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="aspNetAjaxBehavior">
<enableWebScript />
</behavior>
<!--<behavior name="RESTBehavior">
<webHttp helpEnabled="true"/>
</behavior>-->
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
这是我的运营合同。
Namespace Contracts
<ServiceContract()>
Public Interface IWebAnalyticsService
<OperationContract(), WebGet(RequestFormat:=WebMessageFormat.Json)>
Sub SendWaEvent(ByVal eventID As Integer, ByVal eventValue As String, _
ByVal cookieVisitID As String, ByVal cookieVisitorSession As String, _
ByVal HTTPXForwardedServer As String, ByVal HTTPXRewriteURL As String, _
ByVal ScriptName As String, ByVal ServerName As String)
End Interface
End Namespace
我的 Ajax 调用非常简单,但这里是:
$.ajax({
type: 'POST',
crossDomain: true,
url: 'http://localhost:37490/services/webanalyticservice.svc/SendWaEvent',
data: Data, //Data is a JSON wrapper I've previously constructed.
contentType: 'application/javascript;charset=UTF-8'
success: function(result) {DoSomething(result);},
error: HandleError
});
[以下更新]
这总是我们忽略的事情,不是吗?无论如何,在对上面的“直截了当”的 AJAX 调用非常不屑一顾之后,它最终成为了问题。为了让它工作,我不得不将我的 AJAX 调用更改为:
function SendAJAX() {
$.ajax({ type: "GET",
url: URL,
data: Data,
dataType: 'jsonp',
jsonpCallback: 'MyCallBack',
timeout: 10000,
crossDomain: true,
contentType: 'application/json; charset=UTF-8',
success: function (data, textStatus, jqXHR) { WriteSuccess(data, textStatus, jqXHR) },
error: function (jqXHR, textStatus, errorThrown) { WriteError(jqXHR, textStatus, errorThrown) },
complete: function (jqXHR, textStatus) { }
});
}
另外,请注意,如果您要在本地进行测试,则需要将以下内容添加到您的 Web 配置中,否则您将收到 500 错误,指出在经过身份验证的服务中不允许跨域 javascript。
<system.web>
<authentication mode="None" />
</system.web>
您可以在 web.release.config 中使用 删除此属性xdt:transform
。
向@Rajesh 大声喊叫!干得好,伙计!