servicestack,Jquery,Json,Jsonp,servicestack" /> servicestack,Jquery,Json,Jsonp,servicestack" />

这个服务堆栈Web服务或jQuery调用有什么错?

这个服务堆栈Web服务或jQuery调用有什么错?,jquery,json,jsonp,servicestack,Jquery,Json,Jsonp,servicestack,我能够让Service Stack的Hello World示例正常工作,但现在我尝试稍微扩展它,以返回一个自定义的站点对象。我制作了一个简单的测试html文件,它使用jQuery将结果拉回来,但是我没有返回站点对象(我想) 以下是我的Web服务: using Funq; using ServiceStack.ServiceInterface; using ServiceStack.WebHost.Endpoints; using System; namespace My.WebService

我能够让Service Stack的Hello World示例正常工作,但现在我尝试稍微扩展它,以返回一个自定义的站点对象。我制作了一个简单的测试html文件,它使用jQuery将结果拉回来,但是我没有返回站点对象(我想)

以下是我的Web服务:

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)
        {

        }
    }
}
使用Funq;
使用ServiceStack.ServiceInterface;
使用ServiceStack.WebHost.Endpoints;
使用制度;
命名空间My.WebService
{
公共类站点存储库
{
}
公共类网站
{
公共字符串名称{get;set;}
公共字符串Uri{get;set;}//如果您发现它有用,请切换到真正的Uri类
}
公共类SiteService:Service/:RestServiceBase
{
公共站点存储库{get;set;}//由IOC注入
公共对象获取(站点请求)
{
//返回新站点{Name=“Google”,Uri=”http://www.google.com" };
返回新站点响应{Result=newsite{Name=“Google”,Uri=”http://www.google.com"}};
}
}
公共类站点响应
{
公共站点结果{get;set;}
}
公共类SiteAppHost:AppHostBase
{
公共站点apphost()
:base(“站点Web服务”,typeof(SiteService).Assembly)
{
}
公共覆盖无效配置(容器)
{
容器。注册(新站点存储库());
路线
.Add(“/site”)
.Add(“/site/{Id}/”);
}
}
公共类全局:System.Web.HttpApplication
{
受保护的无效应用程序\u启动(对象发送方,事件参数e)
{
新建SiteAppHost().Init();
}
受保护的无效会话\u启动(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u BeginRequest(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u AuthenticateRequest(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u错误(对象发送方,事件参数e)
{
}
受保护的无效会话\u结束(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u结束(对象发送方,事件参数e)
{
}
}
}
下面是使用jQuery的测试HTML文件。我添加了?回调=?因为我同时运行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>

//我们将在这里添加javascript代码
$(文档).ready(函数(){
//当DOM准备好的时候做一些事情
警报(“你好,世界!”);
//回调=?部分用于在与web服务相同的服务器上进行测试
$.getJSON(“http://localhost:61549/site?callback=?,函数(SitesReturn){
警报(SitesReturn);//警报框包含:[对象]
警报(sitesReturned.Name);//警报框包含:未定义
警报(sitesreturn.length==1)//警报框包含:false
var parsed=JSON.parse(sitesreturn);//此操作以静默方式失败
警报(parsed.Name);//警报框不加载
});
警惕(“再见世界!”);
});                                    
你好
一些注意事项

  • 但是我没有得到返回的站点对象(我想)
$.getJSON将有如下响应

{“结果”:{“名称”:“谷歌”,“uri”:”http://www.google.com“}
因此名称/uri属性位于
结果
属性内

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
  • 我同时运行Web服务和从同一台机器拨打电话。
我不太清楚你这是什么意思。如果
http://localhost:61549
您不需要使用JSONP

  • var parsed=JSON.parse(sitesreturn)//这会无声地失败
sitesReturned参数已解析为JavaScript对象,因此此行失败,因为它试图解析对象而不是字符串。见文件。另外,我没有看到引用或
标记,但我假设您使用Douglas Crockford的JSON库来处理
JSON.parse()

来自文档:

“成功回调传递返回的数据,该数据通常是由JSON结构定义的JavaScript对象或数组,并使用$.parseJSON()方法进行解析。它还传递响应的文本状态。”

一些注释

  • 但是我没有得到返回的站点对象(我想)
$.getJSON将有如下响应

{“结果”:{“名称”:“谷歌”,“uri”:”http://www.google.com“}
因此名称/uri属性位于
结果
属性内

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
  • 我同时运行Web服务和从同一台机器拨打电话。
我不太清楚你这是什么意思。如果
http://localhost:61549
您不需要使用JSONP

  • var parsed=JSON.parse(sitesreturn)//这会无声地失败
sitesReturned参数已解析为JavaScript对象,因此此行失败,因为它正在尝试解析对象,而不是
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"}};
        }
    }