警告部分AJAX响应

时间:2014-07-10 16:33:34

标签: javascript jquery ajax json

我使用JS提交数据而不加载页面,它工作正常,响应我试图发送看起来像这样的JSON

{"mes":"<div class=\"alert alert-success\">Your goal has been updated.<\/div>","graph_data":"[['07\/9\/2014',500],['07\/8\/2014',900],['07\/7\/2014',1200],['07\/6\/2014',500],['07\/5\/2014',500],['07\/4\/2014',500],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000]]"}

JSON回复mesgraph_data中有两项。现在我如何单独使用graph_datames

如果我执行此操作alert(data);,则会显示上述JSON响应

但是,如果我做以下情况,我就无法让他们分别提醒。

alert(data.graph_data);
alert(data.mes);

如果有人能指导我如何将两者分开,我将非常感激。

更新

这是我用来点击按钮发送和检索数据的JS

$('#goalgraphdatasubmit').click(function () {
    $('#goalgraphupdateform').submit();
});
$('#goalgraphupdateform').submit(function (e) {
    "use strict";
    e.preventDefault();
    document.getElementById("goalgraphdatasubmit").innerHTML = "saving..";
    var post = $('#goalgraphupdateform').serialize();
    var action = $('#goalgraphupdateform').attr('action');
    $("#holiday_goal_message").slideUp(350, function () {
        $('#holiday_goal_message').hide();
        $.post(action, post, function (data) {
            $('#holiday_goal_message').html(data);
            document.getElementById('holiday_goal_message').innerHTML = data;
            $('#holiday_goal_message').slideDown('slow');
            document.getElementById("goalgraphdatasubmit").innerHTML = "Submit";
            alert(data);
            if (data == '<div class="alert alert-success">Your goal has been updated.</div>') {

                //$('#divGoal').load('dashboard-goals.php');
                $("#holiday_goal_message").hide(2000);
                updatetestGraph();
            }

        });
    });
});

2 个答案:

答案 0 :(得分:1)

使用喜欢

var data = JSON.parse('{"event1":{"title":"My birthday","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "},"event2":{"title":"My birthday again","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "}}');

arr = []
for(var event in data){
    var dataCopy = data[event]
    for(key in dataCopy){
        if(key == "start" || key == "end"){
            // needs more specific method to manipulate date to your needs
            dataCopy[key] = new Date(dataCopy[key])
        }
    }
    arr.push(dataCopy)
}

alert( JSON.stringify(arr) )

Demo1 Demo2

答案 1 :(得分:0)

  

抱歉无法发表评论所以必须回答

我已经在你的变量中使用了你的JSON字符串,它给了我正确的结果 See here

 var d = {"mes":"<div class=\"alert alert-success\">Your goal has been updated.    <\/div>","graph_data":"[['07\/9\/2014',500],['07\/8\/2014',900],['07\/7\/2014',1200],['07\/6\/2014',500],['07\/5\/2014',500],['07\/4\/2014',500],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000],['07\/11\/2014',2000]]"};
 alert(d.graph_data);
 alert(d.mes);