无法通过ajax调用访问服务器中托管的Wcf服务,但可以访问本地计算机中托管的相同Wcf服务
问题描述:
<configuration>
<system.webserver>
<httpprotocol>
<customheaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customheaders>
</httpprotocol>
</system.webserver>
<appsettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="ConnectionString" value="Data Source=ServerDatabase; Initial Catalog=NewDataBase; User ID=pras; Password=rita" />
</appsettings>
<system.web>
<compilation debug="true" targetframework="4.5" />
<httpruntime targetframework="4.5" />
<authentication mode="Windows" />
<rolemanager enabled="true" />
</system.web>
<system.servicemodel>
<bindings>
<webhttpbinding>
<binding name="crossDomain" crossdomainscriptaccessenabled="true" />
</webhttpbinding>
</bindings>
<behaviors>
<endpointbehaviors>
<behavior name="tSeyvaWCFEndPointBehavior">
<webhttp />
</behavior>
</endpointbehaviors>
<servicebehaviors>
<behavior name="tSeyvaServiceBehavior">
<servicemetadata httpgetenabled="true" httpsgetenabled="true" />
<servicedebug includeexceptiondetailinfaults="true" />
</behavior>
</servicebehaviors>
</behaviors>
<protocolmapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolmapping>
<servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true" />
<services>
<service name="AppServices.AppService" behaviorconfiguration="tSeyvaServiceBehavior">
<endpoint address="" behaviorconfiguration="tSeyvaWCFEndPointBehavior">
bindingConfiguration="crossDomain" binding="webHttpBinding"
contract="AppServices.IAppService">
</endpoint>
</service>
</services>
</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" />
<handlers accesspolicy="Read, Execute, Script" />
</system.webserver>
</configuration>
**And this is the ajax call I have made:**
var data = { 'logindetails': { 'UserName': UN, 'Password': PW } };
var st = JSON.stringify(data);
debugger;
$.ajax({
type: "POST",
url: "http://192.168.4.89/mob/AppService.svc/GetUserAuthendication",
data: st,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
ServiceSucceeded(result);
//// Play with response returned in JSON format
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR + "-" + textStatus + "-" + errorThrown);
}
});
When I host my Wcf service in my local IIS and make an ajax call from my local machine itself, its working fine, but when I host my Wcf service in server and make an ajax call to that Wcf service hosted in server, from my local machine, I couldn't access it.
Please go through this code and help me in finding the problem
答
.ajax({
类型: POST,
url:http://192.168.4.89/mob/AppService.svc/GetUserAuthendication,
data:st,
contentType:application / json; charset = utf -8,
dataType:json,
成功:函数(结果){
ServiceSucceeded(result);
////使用JSON格式返回的响应播放
},
错误:函数(jqXHR,textStatus,errorThrown){
alert(jqXHR + - + textStatus + - + errorThrown);
}
});
当我在我的本地IIS中托管我的Wcf服务并从我的本地机器本身进行ajax调用时,它工作正常,但是当我在服务器中托管我的Wcf服务并对该Wcf进行ajax调用时托管服务器的服务,从我的本地机器,我无法访问它。
请仔细阅读此代码并帮助我找到问题
.ajax({ type: "POST", url: "http://192.168.4.89/mob/AppService.svc/GetUserAuthendication", data: st, contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { ServiceSucceeded(result); //// Play with response returned in JSON format }, error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR + "-" + textStatus + "-" + errorThrown); } }); When I host my Wcf service in my local IIS and make an ajax call from my local machine itself, its working fine, but when I host my Wcf service in server and make an ajax call to that Wcf service hosted in server, from my local machine, I couldn't access it. Please go through this code and help me in finding the problem