js跨域调用wcf服务
js跨域调用wcf服务:
1.在Service.cs里添加:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[JavascriptCallbackBehavior(UrlParameterName="jsoncallback")] 
public class Service
{
    [OperationContract]
    [WebGet(RequestFormat = WebMessageFormat.Json)]
public string getChapter(string symbol,string chapter)
{
symbol="A";
chapter="BBBB";
string result=symbol+chapter;
}
}
2.在配置文件里:
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="navMetadataBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" >
      <baseAddressPrefixFilters>
        <add prefix="string"/>
      </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
    <services>
      <service name="ReaderService" behaviorConfiguration="navMetadataBehavior">
        <endpoint address="" behaviorConfiguration="webBehavior"
          binding="webHttpBinding" contract="ReaderService" bindingConfiguration="webBinding" />
        <!--<endpoint address="" binding="basicHttpBinding" contract="ReaderService"  />-->
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding" crossDomainScriptAccessEnabled="true"></binding> //跨域
      </webHttpBinding>
    </bindings>
  </system.serviceModel>
3.在页面:
在文本框输入:两个文本框分别输入:A 和BBBB;
 <script type="text/javascript">
        function GetServerHelloWorldByJsop() {
            var symbol = document.getElementById("symbol").value;
            var chapter = document.getElementById("chapter").value;
           $.getJSON("http://192.168.80.20:90/ReaderService.svc/getChapter?jsoncallback=?",
            { symbol: symbol, chapter: chapter },
             function (data) {
                 alert(data);
             });
        }
    </script>
<body>
   <h2>修改订单</h2> 
   Code: <input type="text" id="symbol" />
    Chapter:   <input id="chapter" type="text" />
  <input type="button" id="btnExcute" name="btnExcute" value="查询"  onclick="GetServerHelloWorldByJsop();"/>
</body>
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号