2012-08-08 58 views
0

我已经设置与我的配置文件设置如下返回JSON格式数据的WCF服务:道场Web应用程序请求WCF服务

<system.web> 
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" /> 
</system.web> 
<system.serviceModel> 
<bindings> 
    <webHttpBinding> 
    <binding name="webHttpBindingJsonP" crossDomainScriptAccessEnabled="true" /> 
    </webHttpBinding> 
</bindings> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="webHttpBehavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="webHttpBehavior"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<services> 
    <service behaviorConfiguration="webHttpBehavior" name="Services.Service1"> 
    <endpoint address="mex" 
     binding="webHttpBinding" bindingConfiguration="webHttpBindingJsonP" 
     contract="Services.IService1" behaviorConfiguration="webHttpBehavior"/> 
    </service> 
</services> 
</system.serviceModel> 
<system.webServer> 
<modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 

我的服务WebInvoke功能:

<OperationContract()> 
<WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.WrappedRequest,  Responseformat:=WebMessageFormat.Json)> 
Function RetrieveData(ByVal screenName As String) As Stream 

最后我基于dojo的网站的功能调用web服务:

<script type="text/javascript"> 

     dojo.ready(function() { 
     dojo.io.script.get({     url: 
      "http://xxx.xxx.x.xxx/Services/Service/Service1.svc/GetData?item=Tweet", 
      callbackParamName: "callback", 
      content: { username: "me", password: "you" } 
      }).then(function (data){ 
        return data.results; 
       }) 
    }); 


</script> 

问题是,我不能让数据流向dojo应用程序。首先,我得到未定义的错误回调。现在我不确定在这个回调函数中我是否清楚:它是dojo应用程序中函数的名称,就像我上面的那样,但函数没有被命名,或者它是函数的名称,返回的是json响应Web服务顺便安装在不同的域上。

回答

0

这是我做的。NET:

我的服务:

 [OperationContract] 
     [WebGet(UriTemplate = "/GetMyStuff", ResponseFormat = WebMessageFormat.Json)] 
     public String GetMyStuff() 
     { 
      var myStuff = getFromService("foo"); 

      return new { label = "name", identifier = "Id", items = myStuff.Select(w => new { Id = w.Id, name = w.Description }) }.ToJSON(); 
     } 

我使用声明为此该ToJSON()辅助函数:

public static class LinqUtils 
{ 
     public static string ToJSON(this object obj) 
     { 
      JavaScriptSerializer serializer = new JavaScriptSerializer(); 

      return serializer.Serialize(obj); 
     } 
} 

在web.config中:

<system.serviceModel> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false"> 
     <serviceActivations> 
     <add relativeAddress="Services/StuffServiceJson.svc" service="MyStuff.MyStuffService" /> <!-- This is an actual URL mapping to your service endpoint --> 
     </serviceActivations> 
    </serviceHostingEnvironment> 

<!-- other stuff --> 

    <services> 
     <service name="MyStuff.MyStuffService"> 
      <endpoint binding="webHttpBinding" contract="MyStuff.MyStuffService" address="" behaviorConfiguration="webHttp"/> <!-- This is a service endpoint to your implementation class mapping --> 
     </service> 
    </services> 
</system.serviceModel> 

在道场:

require(["dojo/_base/xhr"], function(xhr) { 

xhr.get({ 
           url: "/Services/StuffServiceJson.svc/GetMyStuff", 
           handleAs: "json", 
           preventCache: true 
    }).then(function (data) { 
      //Do something with DATA 
    }, function (error) { 
       //Do something with error OMG 
    }); 

}); 

如果你得到的数据被返回的字符串反正(在.NET中有时会发生)问题,那么你得把你的数据,并做

require(["dojo/json"], function(json){ 
    json.parse(data) 
}); 

运气,

+0

感谢您的回应。但xhr不适用于跨域服务器应用程序 – user1585245 2012-08-09 17:05:23

+0

请参阅此链接http://dojo-toolkit.33424.n3.nabble.com/Cross-domain-XMLHttpRequest-with-Dojo-td1864416.html并查看Kris Zyp's响应。 – eburgos 2012-08-10 13:40:54

相关问题