json_encode not working with $.get() [closed]

2019-09-20 06:22发布

I have a file graph.php, where I am writing both the php code and javascript code.I am using flot to plot graph While passing the response, I am using json_ecode($arr); but I am not getting any response for this and the graph disappears while using the same.

<?php
    //connected the mysql and after performing some functions got an array 
    echo json_encode($arr);
?>
<script src="jquery.js"></script>
<script>
    $('#btn'. click(function(){
        $.get("graph.php",function(response){},"json")
    });
</script>

I added header("Content-type :application/json"); While using this, the browser is downloading graph.php and nothing else is coming.

2条回答
Luminary・发光体
2楼-- · 2019-09-20 06:40

You can try some simple array for testing. Not MySql result.

<?php
$arr = array('action' => 'test', 'data' => 'testing');
header("Content-type: application/json");
echo json_encode($arr);
查看更多
男人必须洒脱
3楼-- · 2019-09-20 06:51

Try doing something with the AJAX response, or at least inspecting it:

$('#btn'. click(function () {
    $.get("graph.php")
    .done(function (response) {
        console.log(response);
    })
    .fail(function (err) {
        console.log(err);
    });
});
查看更多
登录 后发表回答