0

次のように、構成ファイルのセットアップでjson形式のデータを返す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

そして最後に、ウェブサービスを呼び出すための私の道場ベースのウェブサイトの機能:

 <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 アプリケーションにデータを流すことができないことです。まず、未定義のエラー コールバックを取得します。今、私はこのコールバックのことについて明確であるかどうか確信が持てません: それは私が上に持っている道場アプリケーションの関数の名前ですか、関数は名前が付けられていませんか、それとも 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 に答える