我已经设置了一个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的网站调用webservice的功能:
<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 :(得分: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)
});
运气,