0

我已经设置了一个 WCF 服务,它使用我的配置文件设置返回 json 格式数据,如下所示:

<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 服务安装在不同的域上。

4

1 回答 1

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)
});

运气,

于 2012-08-08T20:34:28.627 回答