使用 jquery-ajax 调用 wcf rest 服务在 chrome 中完美运行,但我在 mozila 等中没有得到相同的结果,所以请找到下面的代码
这是网络配置...
<?xml version="1.0"?>
<configuration>
<appSettings configSource="appsettings.config"/>
<connectionStrings/>
<system.web>
<compilation targetFramework="4.0" debug="true"/>
<authentication mode="Windows"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.webServer>
<directoryBrowse enabled="true"/>
<defaultDocument>
<files>
<remove value="Default.htm"/>
<remove value="Default.asp"/>
<remove value="index.htm"/>
<remove value="index.html"/>
<remove value="iisstart.htm"/>
<remove value="default.aspx"/>
<add value="DataSync.svc"/>
</files>
</defaultDocument>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>
<system.serviceModel>
<services>
<service name="TonightRoomsSynchronization.DataSync" behaviorConfiguration="jsonServiceBehaviour">
<endpoint address="JSON" binding="webHttpBinding" contract="TonightRoomsSynchronization.IDataSync" behaviorConfiguration="web" bindingConfiguration="StreamedRequestWebBinding"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="jsonServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="StreamedRequestWebBinding" crossDomainScriptAccessEnabled="true" bypassProxyOnLocal="true" useDefaultWebProxy="false" hostNameComparisonMode="WeakWildcard" sendTimeout="10:15:00" openTimeout="10:15:00" receiveTimeout="10:15:00" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" transferMode="StreamedRequest">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
<wsHttpBinding>
<binding name="NewBinding0" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" maxBufferPoolSize="524288000" maxReceivedMessageSize="655360000" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</wsHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
</configuration>'
和 HTML 页面中的 ajax 调用如下..
jQuery.support.cors = true
function AssignValue() {
debugger;
Type= "POST";
Url = "http://ipaddress/TestTonightRoomsSynchronization/DataSync.svc/JSON/login/" + $("#txtUsername").val() + "/" + $("#txtPassword").val();
Data = '{}';
DataType = "JSON";
ContentType = "application/json; charset=UTF-8";
ProcessData = true;
CallService();
}
function CallService() {
$.ajax({
type: Type,
data: Data,
dataType: DataType,
processData: ProcessData,
url: Url,
contentType: ContentType,
crossDomain: true,
success: function (jsonObj) {
alert('success');
},
error: function (response) {
alert('Error while sending the request');
}
});
}'