简体   繁体   English

WebMethod返回JSON对象问题

[英]WebMethod returning JSON object issue

I am struggling to get object handled properly when returned from WebMethod. 从WebMethod返回时,我正努力使对象得到正确处理。 Conditions (my research after it): 条件(我的研究之后):

Simple class to hold the properties: 用于保存属性的简单类:

    public class memberLogin
    {
    public string Username {get; set;}
    public string Password {get; set;}
    }

Serialization method: 序列化方法:

        public static string JsonSerializer<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, t);
            string jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }

(simplified for clearness) Webmethod (为清楚起见简化了)Web方法

    [WebMethod(EnableSession = true)]
    public static string getCredentials()
        {
            memberLogin ml = new memberLogin();
            ml.Username = "user";
            ml.Password = "pass";
            string json = JsonHelper.JsonSerializer<memberLogin>(ml);
            return json;
        }
        return null;
    }

And JavaScript 和JavaScript

$("#element").live("click", function (e) { 

$.ajax({
    type: "POST",
    url: "Default.aspx/getCredentials",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    cache: false,
    success: post_to_url
});
});
function post_to_url(params) {
method = "post";
path = "http://example";

var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);

for(var key in params) {
    if(params.hasOwnProperty(key)) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
     }
}
document.body.appendChild(form);
form.submit();
}

When debugging, WebMethod returns "{\\"Password\\":\\"pass\\",\\"Username\\":\\"user\\"}" so it seems to be what I want it to be (key, value par), except the \\ but that is how it is serialized. 调试时,WebMethod返回"{\\"Password\\":\\"pass\\",\\"Username\\":\\"user\\"}"所以这似乎是我想要的(键,值par),除了\\但这就是序列化的方式。 However when retrieved using ajax, I can not get any values out of it. 但是,当使用ajax检索时,我无法从中获得任何值。 alert(params) gives object Object , call to params.Username gives undefined . alert(params)给出object Object ,调用params.Username给出undefined Is there something I am missing? 我有什么想念的吗? Some sort of cast? 某种类型的演员? I thought that if object is serialized, then there's no such need. 我以为如果将对象序列化,则没有这种需要。 Sorry for very long post but I tried to provide as much informations as I could. 抱歉,很长的帖子,但我试图提供尽可能多的信息。

You have to parse the JSON String into a JSON object. 您必须将JSON字符串解析为JSON对象。 In Javascript: 用Javascript:

var user = JSON.parse(params.responseText);
var username = user.Username;
var password = user.Password;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM