English 中文(简体)
Json Eval 无法分析数据
原标题:Json eval not able to parse data
  • 时间:2012-05-24 16:33:53
  •  标签:
  • php
  • jquery

我有一个 php 程序, 在那里我测试一些样本数据。 我在元素列表后面丢失了 < code> 错误。 我如何读读这个?

$dataDetailsList = array();
array_push($dataDetailsList, array( a  => 1 , b  => 2 , c  => 3 , d  => 4 , e  => 5 ));
echo json_encode(array("DataDetailsList"=>$dataDetailsList));

然后在我的jQuery处理器 我这样做。

function requestData() {
    $.ajax({
        url:  live-server-data.php ,
        success: function(data) {
            //alert(json);
            var jsonData = eval(" (" + data + ") ");
        },
        cache: false
    });
最佳回答

Don t use eval is evil. Instead of this use:

JSON.parse(data); // not supported in IE7 and below

我觉得你需要试试

dataType:  json 

也就是说,

$.ajax({
    url:  live-server-data.php ,
    dataType:  json ,
    success: function(data) {
        var jsonData = data;
        console.log(jsonData);
        $.each(jsonData.DataDetailsList, function(key, val) {
             var key = Object.keys(val)[0],
                 value = val[key];
             console.log(key); // output: a, b, c ...
             console.log(value); // output: 1, 2, 3,...
            // alternative
            for(var key in val) {
                console.log(key);
                console.log(val[key]);
            }
        });
    },
    cache: false
})
问题回答
function requestData() {
    $.ajax({
        url:  live-server-data.php ,
        success: function(data) {
            //alert(json);
            var jsonData = data;

        },
        cache: false,
        dataType:  json  //data type that it will return 
    });
}

您应该将 < code> dataType 设置为json, JQuery 将为您提供这个技巧 。

$.ajax({
    url:  live-server-data.php ,
    dataType:  json ,  //Added dataType json
    success: function(data) {
        //Now data is a javascript object (JSON)
    },
    cache: false
});




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签