ASP.Net ScriptMethod产生空JSON [英] ASP.Net ScriptMethod generating empty JSON

查看:106
本文介绍了ASP.Net ScriptMethod产生空JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JavaScript访问称为GetPerson一个ScriptService()方法。
的问题是,它不是返回一个实际的对象的字符串的一个相当空JSON字符串。
当我返回一个新的DateTime对象同样的情况,所以类是毫无疑问我希望的。

这将返回:

  {D:{__类型:人}}

这是我的WebService.cs:

 使用系统;
System.Collections中使用;
使用System.Linq的;
使用的System.Web;
使用System.Web.Services;
使用System.Web.Services.Protocols;
使用System.Xml.Linq的;
使用System.Web.Script.Services;///<总结>
///< /总结>
[WebService的空间(namespace =htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[ScriptService]
公共类的WebService:System.Web.Services.WebService
{
    公众的WebService(){
        //的InitializeComponent();
    }    [的WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)
    公众人物GetPerson(串什么)
    {
        人X =新的Person(加加,DateTime.Now,NULL);
        返回X;    }
}


解决方案

所以事实证明,这的确是我的同班同学文件有问题。工程与公共属性要好很多。

 公共字符串名称{;组; }

2的个小时的调试是对什么诱发#1灵感。卫生署。

I'm using JavaScript to access a ScriptService method called GetPerson(). The problem is that it is returning a fairly empty JSON string instead of an actual object string. The same happens when I return a new DateTime object so the class is out of question I hope.

This is returned:

{"d":{"__type":"Person"}}

this is my WebService.cs:

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;

/// <summary>
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
    public WebService () {
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public Person GetPerson(string whatever)
    {
        Person x = new Person("gaga",DateTime.Now,null);
        return x;

    }
}

解决方案

And so it turns out that this was indeed a problem with my class file. Works a lot better with public properties.

public string name { get; set; }

2 hours of debugging is nothing vs. Stackoverflow induced inspiration. doh.

这篇关于ASP.Net ScriptMethod产生空JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆