【问题标题】:how to get the items base in json objects如何在 json 对象中获取项目库
【发布时间】:2014-07-18 15:41:00
【问题描述】:

我在 javascript 中的 ajax POST 之后有这个返回

Object {d: "{"Success":true,"Message":"success test"}"}

我想获取 Success 值和 Message 值。 知道如何在 javascript 和 knockoutjs 中执行此操作吗?

这是ajax post的代码:

$.ajax({
        type: "POST",
        contentType: "application/json",
        url: "ManualOfferEx.aspx/OnSubmit",
        data: JSON.stringify(data),
        dataType: "json",
        success: function (result) {
            console.log(result);
            var data = result.d;
            console.log(ko.toJS(data));
            //console.log(data);
        },
        error: function (xhr, err) {
            console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText);
        }
    });

【问题讨论】:

标签: javascript json knockout.js


【解决方案1】:

看来这应该适合你:

$.ajax({
        type: "POST",
        contentType: "application/json",
        url: "ManualOfferEx.aspx/OnSubmit",
        data: JSON.stringify(data),
        dataType: "json",
        success: function (result) {
            console.log(result);
            var data = result.d;
            var success = data.Success;
            var message = data.Message;
            console.log(message);
        },
        error: function (xhr, err) {
            console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText);
        }
    });

此外,您还调用了 ko.toJS()。仅当您尝试将 Knockout 模型/对象转换为 JS 对象时才需要这样做。由于你是在进行 Ajax 调用,所以调用的结果已经是一个 JS 对象了。

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 2021-12-25
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多