ASP.Net ScriptMethod 生成空 JSON
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明这确实是我的类文件的问题。与公共财产配合得更好。
与 Stackoverflow 引发的灵感相比,2 小时的调试不算什么。哦。
And so it turns out that this was indeed a problem with my class file. Works a lot better with public properties.
2 hours of debugging is nothing vs. Stackoverflow induced inspiration. doh.