ASP.Net ScriptMethod generating empty JSON

独自空忆成欢 提交于 2019-12-20 04:13:45

问题


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;

    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/1791088/asp-net-scriptmethod-generating-empty-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!