无法通过ajax调用访问服务器中托管的Wcf服务,但可以访问本地计算机中托管的相同Wcf服务 [英] Couldn't access Wcf service hosted in a server through ajax call, but can access the same Wcf service hosted in local machine

查看:95
本文介绍了无法通过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"&gt;
        </endpoint>
      </service>
    </services>
  </system.servicemodel>
 <system.webserver>
    <modules runallmanagedmodulesforallrequests="true" />
    &lt;!--
        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.
      --&gt;
    <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


这篇关于无法通过ajax调用访问服务器中托管的Wcf服务,但可以访问本地计算机中托管的相同Wcf服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆