ASP.Net ScriptMethod 生成空 JSON

发布于 2024-08-12 06:22:51 字数 956 浏览 4 评论 0原文

我正在使用 JavaScript 访问名为 GetPerson() 的 ScriptService 方法。 问题是它返回一个相当空的 JSON 字符串,而不是实际的对象字符串。 当我返回一个新的 DateTime 对象时也会发生同样的情况,因此我希望该类没有问题。

返回:

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

这是我的 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;

    }
}

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;

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病女 2024-08-19 06:22:51

事实证明这确实是我的类文件的问题。与公共财产配合得更好。

public string name { get; set; }

与 Stackoverflow 引发的灵感相比,2 小时的调试不算什么。哦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文