dtc88867 2017-10-15 16:57
浏览 207

eval()函数不起作用

im trying to convert a json string from my .php file using eval() function but it doesnt work. the browser console says SyntaxError: expected expression, got '<'...

but when i comment out the line where eval() is, and use document.write(data); the string appears...

here's my code..

<html>
<head>
    <script type='text/javascript' src='jquery.js'></script>
    <script type='text/javascript'>

    var go = function() {

        $.get("testjson.php", function(data) {

            var obj = eval("(" + data + ")");

            document.write(obj.name + "<br />");
            document.write(obj.date + "<br />");
        });
    }
    </script>
</head>
<body>
    <input type='button' value='go' onclick='go()' />
<body>
</html>

and here's the code of my testjson.php file...

<?php

    $msg = array(
        "name"=>"hi there Victor!",
        "date"=>"Monday 21st Feb 2010"
    );

    $myMsg = json_encode($msg);

    echo $myMsg;

?>

im using latest version of jquery..

  • 写回答

2条回答 默认 最新

  • dongxing2692 2017-10-15 17:10
    关注

    There are other suggestions in the comments and answers here about using $.getJSON() instead of eval(), or specifying json as a parameter in $.get(), and these are all good suggestions. But, they aren't the reason this isn't working.

    Simply, you're missing a semi-colon in your var go = .... function definition after the closing brace near the bottom.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题