2017-06-16 96 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)); 

     }