我能够使Service Stack的Hello World示例正常工作,但现在我正在尝试扩展它以返回自定义的 Site 对象。我做了一个简单的测试html文件,它使用jQuery来取回结果,但我没有返回 Site 对象(我认为)。
这是我的网络服务:
using Funq;
using ServiceStack.ServiceInterface;
using ServiceStack.WebHost.Endpoints;
using System;
namespace My.WebService
{
public class SiteRepository
{
}
public class Site
{
public string Name { get; set; }
public string Uri { get; set; } //switch to the real uri class if you find it useful
}
public class SiteService : Service //: RestServiceBase<Site>
{
public SiteRepository Repository { get; set; } //Injected by IOC
public object Get(Site request)
{
//return new Site { Name = "Google", Uri = "http://www.google.com" };
return new SiteResponse {Result = new Site {Name = "Google", Uri = "http://www.google.com"}};
}
}
public class SiteResponse
{
public Site Result { get; set; }
}
public class SiteAppHost : AppHostBase
{
public SiteAppHost()
: base("Site Web Services", typeof(SiteService).Assembly)
{
}
public override void Configure(Container container)
{
container.Register(new SiteRepository());
Routes
.Add<Site>("/site")
.Add<Site>("/site/{Id}/");
}
}
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new SiteAppHost().Init();
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
这是使用jQuery的测试HTML文件。我添加了?callback =?因为我正在运行Web服务和从同一台机器进行调用。
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
// we will add our javascript code here
$(document).ready(function() {
// do stuff when DOM is ready
alert("Hello world!");
//the ?callback=? part is for testing on the same server as the web service
$.getJSON("http://localhost:61549/site?callback=?", function(sitesReturned) {
alert(sitesReturned); // alert box contains: [object Object]
alert(sitesReturned.Name); //alert box contains: undefined
alert(sitesReturned.length == 1) //alert box contains: false
var parsed = JSON.parse(sitesReturned); //this fails silently
alert(parsed.Name); // the alert box does not load
});
alert("Goodbye world!");
});
</script>
</head>
<body>
<!-- we will add our HTML content here -->
Hello
</body>
</html>
答案 0 :(得分:1)
一些笔记......
$。getJSON会有一个看起来像这样的响应
{"result":{"name":"Google","uri":"http://www.google.com"}}
所以名称/ uri属性位于result
属性中。
alert(sitesReturned.result); // will still contain [object Object]
alert(sitesReturned.result.name); //should contain Google
alert(sitesReturned.result.uri.length == 1) //contains false since 21 != 1
不完全确定你的意思。如果http://localhost:61549
提供包含jQuery代码的HTML文件,则不需要使用JSONP。
sitesReturned参数已经被解析为JavaScript对象,因此该行失败,因为它试图解析一个对象而不是一个String。请参阅文档here。此外,我没有看到引用或<script>
标记,但我假设您使用Douglas Crockford的JSON.parse()
JSON库。
来自文档:
“成功回调传递返回的数据,该数据通常是JSON结构定义的JavaScript对象或数组,并使用$ .parseJSON()方法进行解析。它还传递响应的文本状态。”
答案 1 :(得分:0)
我想出来了。愚蠢的错误。
public class SiteService : Service //: RestServiceBase<Site>
{
public SiteRepository Repository { get; set; } //Injected by IOC
public object Get(Site request)
{
//return new Site { Name = "Google", Uri = "http://www.google.com" };
return new SiteResponse {Result = new Site {Name = "Google", Uri = "http://www.google.com"}};
}
}
应该是
public class SiteService : IService //: RestServiceBase<Site>
{
public SiteRepository Repository { get; set; } //Injected by IOC
public object Get(Site request)
{
//return new Site { Name = "Google", Uri = "http://www.google.com" };
return new SiteResponse {Result = new Site {Name = "Google", Uri = "http://www.google.com"}};
}
}
我需要添加以下命名空间:using ServiceStack.ServiceHost;