2017-06-16 57 views
0

我想建立一個WCF服務,將返回JSONP請求。我希望它返回以下內容,如何從WCP請求返回JSONP?

jsonpCallback({「fileNames」:「IDR023.T.201705201412.png,IDR023.T.201705201418.png」});

所以我創建了下面的WCF服務

IBOM.cs

[ServiceContract] 
    public interface IBOM 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json)] 
     string GetData(); 
    } 

BOM.cs

public class BOM : IBOM 
    { 

     public string GetData() 
     { 
      return "jsonpCallback({\"fileNames\":\"IDR023.T.201705201412.png, IDR023.T.201705201418.png\"});"; 


     } 
    } 

Webconfig

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="BomService.BOM" behaviorConfiguration="ServiceBehaviour"> 
     <!-- Service Endpoints --> 
     <!-- Unless fully qualified, address is relative to base address supplied above --> 
     <endpoint address ="" binding="webHttpBinding" contract="BomService.IBOM" behaviorConfiguration="web"> 
      <!-- 
       Upon deployment, the following identity element should be removed or replaced to reflect the 
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
       automatically. 
      --> 
     </endpoint> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehaviour"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

但我找回

「jsonpCallback({\」 文件名\ 「:\」 IDR023.T.201705201412.png,IDR023.T.201705201418.png \ 「});」

我返回一個字符串,並試圖將其設置返回我需要什麼。這是JSONP的最佳方式嗎?或者是有沒有更好的方式來實現我需要什麼

感謝

回答

0

對於任何人誰是有興趣的,我的工作了。這是代碼。

伊博姆

[的ServiceContract] 公共接口伊博姆 {

[OperationContract] 
[WebInvoke(Method = "GET", 
    ResponseFormat = WebMessageFormat.Json)] 
Stream GetData(); 

}

BOM

public class BOM : IBOM 
    { 
     public Stream GetData() 
     { 


      string jsCode = "jsonpCallback" + "({\Test:\"" + fileNames + "\"});"; 
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/javascript"; 
      return new MemoryStream(Encoding.UTF8.GetBytes(jsCode)); 

     }