douzhai1182
2017-06-18 01:37
浏览 45

从JavaScript调用PHP JSON数据

I am attempting to obtain PHP data from my Javascript code. This is meant to be shown in the dayta div id. What I end up getting instead is undefined instead of the desired result (which in this instance is the firstname in my JSON file). I am not sure why this is happening.

My JavaScript code:

window.onload = function(){
    var http = new XMLHttpRequest();
    http.onreadystatechange = function(){
        if (http.readyState == 4 && http.status == 200){
            var dayta = document.getElementById('datadisplay');
                dayta.innerHTML = http.response.firstname;
        }
    }
    http.open("GET", "myphpfile.php", true);
    http.send();
}

My PHP code (myphpfile.php):

<?php   
    $content = file_get_contents('somejsonfile.json');
    print_r($content);
    $dbcon->close();
?>

The PHP code included is only a portion of my code, but it contains all the parts relevant to my problem.

$content outputs a JSON file, as that is what it is pointed to. If I echo $content I am able to verify that the content is streamed out as intended.

Thus, the problem is most likely with my JavaScript code. But I cannot identify specifically what is wrong.

My JSON file (somejsonfile.json):

{"firstname": "Ki ki ki ma ma ma"}

I understand that the most obvious way to approach this specific example is to simply have the JavaScript file reach the JSON file. However, since I'm trying to secure the path of my JSON file in my production code I am trying out this method.

图片转代码服务由CSDN问答提供 功能建议

我试图从我的Javascript代码中获取PHP数据。 这应该显示在 dayta div id中。 我最终得到的是 undefined 而不是期望的结果(在这个例子中是我的JSON文件中的 firstname )。 我不确定为什么会这样。

我的 JavaScript 代码:

  window.onload = function(){
 var http = new  XMLHttpRequest(); 
 http.onreadystatechange = function(){
 if if(http.readyState == 4&amp;&amp; http.status == 200){
 var dayta = document.getElementById('datadisplay');  
 dayta.innerHTML = http.response.firstname; 
} 
} 
 http.open(“GET”,“myphpfile.php”,true); 
 http.send(); 
} 
    
 
 

我的 PHP 代码( myphpfile.php ):

 <  code>&lt;?php 
 $ content = file_get_contents('somejsonfile.json'); 
 print_r($ content); 
 $ dbcon-&gt; close(); 
?&gt; 
    
 
 

包含的PHP代码只是我代码的一部分,但它包含与我的问题相关的所有部分。

$ content 输出一个JSON文件,就像它所指向的那样。 如果我回显 $ content ,我可以验证内容是否按预期流出。

因此,问题很可能出在我的JavaScript代码上。 但我无法确切地指出什么是错的。

我的 JSON 文件( somejsonfile.json ):

  {“  firstname“:”Ki ki ki ma ma ma“} 
   
 
 

我理解,接近这个具体示例最明显的方法就是让JavaScript文件到达 JSON文件。 但是,因为我正在尝试在我的生产代码中保护我的JSON文件的路径,所以我正在尝试这种方法。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douqingzhi0980 2017-06-18 01:44
    已采纳

    Make sure you get a JSON response by using JSON.parse. Here's a quick solution. I have tried it and it works great!

    window.onload = function(){
        var http = new XMLHttpRequest();
        http.onreadystatechange = function(){
            if (http.readyState == 4 && http.status == 200){
                var dayta = document.getElementById('datadisplay');
                var data = JSON.parse(http.responseText);
                dayta.innerHTML = data.firstname;
            }
        }
        http.open("GET", "myphpfile.php", true);
        http.send();
    }
    
    已采纳该答案
    打赏 评论
  • duanbei3747 2017-06-18 02:03

    pls check this code;

        <?php   
            $content = file_get_contents('somejsonfile.json');
            print_r(json_decode($content));
        ?>
    

    if you see array truly then,

    <?php   
        $content = file_get_contents('somejsonfile.json');
        echo(json_encode($content));
    ?>
    

    if you want to query in json file, you can use json_decode function to convert array and push or remove array item and then json_encode.

    打赏 评论

相关推荐 更多相似问题