JSONP 跨域請求WCF


模擬建兩個項目,一個WcfService,一個 Asp.Net

一、Service WCF

1、IService1.cs 注意要加上注解[WebGet()],否則客戶端不能訪問到

        [OperationContract]
        [WebGet()]
        Stream GetData(string callback);

2、Service1.svc

  注意加上[AspNetCompatibilityRequirements]注解,

 返回類型以Stream 代替string,避免返回的內容帶有雙引號,比如若返回 "call({msg:'Hello'})",客戶端就不會執行ajax里的success或者自定義的callback函數。正確的是返回不帶雙引號的
call({msg:'Hello'}) 這樣客戶端會去執行call函數。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  
    public class Service1 : IService1
    {
        public Stream GetData(string callback)
        {
             string jsCode = callback+"({msg:'Hello,I am from WCF'})";
             return new MemoryStream(Encoding.UTF8.GetBytes(jsCode));

            // return "call({msg:'Hello,I am from WCF'})";
        }
    }

3、Web.config.注意高亮部分的配置 binding="webHttpBinding"

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  
  <system.serviceModel>
    <services>
      
      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="WcfService1.EndBehavior">       
        </endpoint>
      </service>
      
    </services>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.Service1Behavior">
          
          <!-- 為避免泄漏元數據信息,請在部署前將以下值設置為 false 並刪除上面的元數據終結點 -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 要接收故障異常詳細信息以進行調試,請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="WcfService1.EndBehavior">          
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      
    </behaviors>
    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

 

二、Web Ajax

 <script type="text/javascript">
        function getData() {
 

            var url1 = "http://localhost:6199/WebForm1.aspx?callback=?";
            jQuery.getJSON(url1, function (data) { alert("執行了success 1:     " + data.msg); });

            var url2 = "http://localhost:6199/Service1.svc/GetData?callback=?";
            $.getJSON(url2, function (data) { alert("執行了success 2:     " + data.msg); });


            $.ajax({
                type: "get",
                url: "http://localhost:6199/Service1.svc/GetData",
                dataType: "jsonp",
                success: function (data) { alert("執行了success 3:   " + data.msg); },
                error: function (error) {
                    alert("調用出錯");
                }
            });


            $.ajax({
                type: "get",
                url: "http://localhost:6199/Service1.svc/GetData",
                dataType: "jsonp",
                jsonpCallback: "call",
                success: function (data) { alert("這里不會執行!"); },
                error: function (error) {
                    alert("調用出錯");
                }
            });

        }
        function call(data) {
            alert("執行了call:     " + data.msg);
        }

        getData();
    </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM