如何在json对象中获得项目基础

how to get the items base in json objects

本文关键字:项目 json 对象      更新时间:2023-09-26

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);
        }
    });

似乎这应该为您工作:

$.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对象。