2012-08-08 57 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

相關問題