如何从ajax数据对象中获取数据?

时间:2017-09-25 09:23:32

标签: javascript jquery ajax

我正在尝试从数据库中以json对象的形式获取日期格式。代码就像

                   $.ajax({
                    url: 'index.php?action=getDateFormat',
                    type: 'POST',
                    dataType: 'JSON',
                    success: function(data){
                        if (data.length) 
                        {
                           var jsonData = JSON.parse(data);
                           return jsonData["DateFormat"];
                        }
                      }
                });

请注意,在浏览器的控制台中,数据为“Object {dateFormat:”d-m-Y“}”。

上面的代码返回“undefined”。请帮忙!!!

在PHP代码中

         $dateFormatArray = array("dateFormat" => $dateFormat);
         json_encode( $dateFormatArray);

在浏览器的控制台中,数据的结果是

         Object { dateFormat: "d-m-Y" }

完整代码

         function getDateFormat()
            {
                var dateFormat ="";
                 $.ajax({
                    url: 'index.php?action=getDateFormat',
                    type: 'POST',
                    dataType: 'JSON',
                    success: function(data)
                    {
                        dateFormat = data.dateFormat;
                    }
                });

                return dateFormat; 
            }

0 个答案:

没有答案