PHP and jquery json

My html file:

    <script>
    $(document).ready(function() {  
        $.ajax({
            type: "POST",
            url: "search.php",
            data: "id=1",
            datatype: "json",
            success: function(msg){
                $('.result1').html(msg["name"]);
            }
        });  
    })
    </script>   

    <span class="result1"></span>

My php file:

    <?
    $a["name"] = 'john';
    echo json_encode($a);
    ?>

Why doesn't the name John appear in the class result1? What for? Please help me, I'm losing my mind.

edit: Can generosity be done right now?

+3
source share
3 answers

The parameter dataTypeis T. It works if you fix it.

Currently (by default) is trying to guess the response format based on the mime type, so probably the default html is debugging in firebug, you can see that msgthe success callback argument is a string containing JSON.

+5
source

. .getJSON() http://api.jquery.com/jQuery.getJSON/. , .

, JSON, , data: "id=1" data: "{id:1}"

, , : msg[0].name;, each() .

0

I think you should use:

 $('.result1').html(msg.name);
-2
source

Source: https://habr.com/ru/post/1757649/


All Articles