Javascript 用ajax使用js获取php

Javascript 用ajax使用js获取php,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,它不会给我从php中的$scoresArray到js中的data_response的值 如果我记录obj,它将不返回任何内容 我有JS的 $.ajax({ url: "Database.php", type: "POST", dataType: "json", success: function (obj) { stageRef.

它不会给我从php中的$scoresArray到js中的data_response的值 如果我记录obj,它将不返回任何内容

我有JS的

  $.ajax({
                url: "Database.php",
                type: "POST",
                dataType: "json",
                success: function (obj) {
                  stageRef.$("txtTopscorePunt").html(obj.score[0]);
                  stageRef.$("txtTopscorePunt2").html(obj.score[1]);
                  stageRef.$("txtTopscorePunt3").html(obj.score[2]);
                }       
           });
这在php中是:

function GetScores(){           
        $query = "SELECT * FROM topscores ORDER BY Scores DESC LIMIT 3";
        $result = mysql_query($query);      

        $scoresArray = array(); 
        $i = 0;
        while ($row = mysql_fetch_assoc($result)) {

            $scoresArray[$i]['score'] = $row['score'];
            $i++;
        }
        echo json_encode($scoresArray); 
    }
您不需要将JSON.parsestr的数据类型设置为:JSON,它已经被解析了

此外,您的选择器是错误的。如果要选择ID,则需要该字符

           $.ajax({
                url: "Database.php",
                type: "POST",
                dataType: "json",
                success: function (obj) {   // not 'complete'
                    console.log(obj);
                    $("#txtTopscorePunt").html(obj.score[0]);
                    $("#txtTopscorePunt2").html(obj.score[1]);
                    $("#txtTopscorePunt3").html(obj.score[2]);
                }       
           });

将代码更新为:

$.ajax({
                url: "Database.php",
                type: "POST",
                dataType: "json",
                success: function (data_response) {
                    console.log(data_response);
                    var response = data_response;
                    var response_str = JSON.stringify(response);
                    alert(response_str); // don't do this if the returned is too much
                    console.log(response_str); // use your browser console to view the results
                    }       
                });
完成此操作后,可以发布警报或控制台日志的结果

另外,在结果中,它是否以[或{开头

从结果中,我们应该能够帮助您以正确的方式访问JSON对象


关于。

使用$.ajax{}.completedata{u response或使用success而不是那样完成。so$.ajax{}.completedata{u response var str=data\u response;var obj=JSON.parsestr;stageRef.$txtopCorepunt.htmlobj.score[0];stageRef.$txtopCorepunt2.htmlobj.score[1];stageRef.$txtopCorepunt3.htmlobj.score[2];stageRef是什么。应该做什么?在回答问题后,请不要实质性地更改问题。stageRef是指向我的stage的链接。我正在使用Adobe edge动画。我将代码更改为您的代码,请参见上文。但我没有登录如果您直接在浏览器中打开PHP页面,您是否看到JSON数据?不,不是。这不是一个问题类或id。日志仍然不会返回任何内容。我已使用stageRef更新了其他文本。$txtopCorepunt.html…因此,这是正确的。我认为问题在于objI没有使用Adobe Edge的经验,因此我无法帮助您完成该部分。非常奇怪,这仍然不会返回任何内容。如果我尝试{},则会使用[]Adobe edge返回我一个错误嗯,这很奇怪。你确定它会成功吗:你能添加错误吗:还有。你能用GET而不是POST来尝试它吗,因为我找不到你在链接中发布任何数据。还有一个调试技巧,让PHP文件先返回一个简单的字符串,然后用Ajax来捕获它。让我知道它是怎么回事。是的,呃ror返回了一些东西。但是为什么会出错呢?我测试了sql语句,它们都工作了。错误是什么?你能在你的PHP文件中放置调试语句吗?对不起,但是你能发布你试图运行的确切JS吗?我可以告诉你变量obj发生了什么