JQUERY函数在返回JSON数据后停止执行

时间:2014-11-22 19:56:41

标签: jquery html ajax json

我正在尝试运行此函数来获取JSON数据并在图表上绘制。我已经获得了JSON,但是在成功之后:AJAX调用我插入了一个警报并且不知何故没有被击中。

任何人都可以告诉我我哪里出错了

这是我的JSON

{
    "t": "radioiodine",
    "y": 50,
    "x": "2014-04-04"
}{
    "t": "radioiodine",
    "y": 50,
    "x": "2013-06-09"
}{
    "t": "radioiodine",
    "y": 100,
    "x": "2012-03-05"
}{
    "t": "radioiodine",
    "y": 200,
    "x": "2008-05-04"
}{
    "t": "radioiodine",
    "y": 100,
    "x": "2006-02-17"
}{
    "t": "radioiodine",
    "y": 50,
    "x": "2002-03-31"
}{
    "t": "radioiodine",
    "y": 100,
    "x": "2001-02-23"
}{
    "t": "radioiodine",
    "y": 50,
    "x": "2000-12-13"
}{
    "t": "radioiodine",
    "y": 100,
    "x": "2000-04-06"
}

这是我的代码

$(document).ready(function(){ 

    $("#find").click(function(e){

                e.preventDefault();


        $.ajax({
            // the URL for the request
            url: "getrad.php",
            // the data to send (will be converted to a query string)
            data: {pnhsno: $('#search').val()},
            // whether this is a POST or GET request
            type: "GET",
            // the type of data we expect back
            dataType : "json",
            // code to run if the request succeeds;
            // the response is passed to the function
            success: function(json){
                    alert("start");
                if(json.length !=0 ){

                    var dataPoints = json.map(function (p) {
                    p.x = new Date(p.x);
                    return p;
                    });

                    //var dp1 = []; 
                    //var lt1; 

                    //for(var i=0; i<dataPoints.length; i++){
                    //  if(dataPoints[i].t =="radioiodine"){
                    //  lt1 =dataPoints[i].t;
                    //  dp1.push({x:dataPoints[i].x, y:dataPoints[i].y})}


                    //}

                        //   $("#radioiodine").CanvasJSChart({ //Pass chart options
                        //      title:{text:"Radioiodine Dosage"},
                            //   zoomEnabled: true,
                            //    panEnabled: true, 
                            //  axisX:{valueFormatString:"DD-MM-YYYY",labelAngle:-45},
        //
                            //  data: [
                                //  {
                                //  type: "column",
                                //  legendText:lt1, 
                                //  showInLegend:true,
                                //  dataPoints:dp1
                                //  }]}); 

                }//if

                //else{ $("#radioiodine").html("No Data For Radioiodine Found");}
            }//json
});
});
});

1 个答案:

答案 0 :(得分:2)

因为您提供的json格式无效。有效的JSON将如下所示:

[{
    "t": "radioiodine",
    "y": 50,
    "x": "2014-04-04"
},{
    "t": "radioiodine",
    "y": 50,
    "x": "2013-06-09"
},{
    "t": "radioiodine",
    "y": 100,
    "x": "2012-03-05"
},{
    "t": "radioiodine",
    "y": 200,
    "x": "2008-05-04"
},{
    "t": "radioiodine",
    "y": 100,
    "x": "2006-02-17"
},{
    "t": "radioiodine",
    "y": 50,
    "x": "2002-03-31"
},{
    "t": "radioiodine",
    "y": 100,
    "x": "2001-02-23"
},{
    "t": "radioiodine",
    "y": 50,
    "x": "2000-12-13"
},{
    "t": "radioiodine",
    "y": 100,
    "x": "2000-04-06"
}]

最有可能的是,success没有被击中,因为你的ajax调用永远不会成功。