php `header("Content-type:application/json");`的用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35284986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:25:18  来源:igfitidea点击:

The usage of `header("Content-type:application/json");`

phpjqueryjsonajax

提问by januaryananda

I just created an JQuery ajaxfunction to retrieve some json-encodeddata from PHP, here's my code :

我刚刚创建了一个 JQueryajax函数来从中检索一些json-encoded数据PHP,这是我的代码:

file name : bank.php

文件名:bank.php

$('form').on('submit', function(){

        var datatobesent  = $(this).serialize();
        $.ajax({
            data: datatobesent,
            url:'data.php',
            type:'GET'
        })
        .done(function(data){
            console.log(typeof(data));
        });
        return false;
})

and in data.phpI wrote

data.php我写的

if(isset($_GET)){
    $data = $_GET;
    echo json_encode($data);
    header("Content-type:application/json");
}

the question is, when I delete the line of header("Content-type:application/json");in data.phpthe console.logtell that the type of data returned by ajaxis string.

问题是,当我删除的行header("Content-type:application/json");data.phpconsole.log告诉数据返回的类型ajaxstring

And when I added dataType :json`` inside the ajaxfunction in bank.phpthe type changes into object

当我dataType :ajax函数中添加json``时bank.php,类型更改为object

so what is the function of header("Content-type:application/json");actually?

那么header("Content-type:application/json");实际上的功能是什么?

回答by Ahmed Essam

The function header("Content-type:application/json")sends the http json header to the browser to inform him what the kind of a data he expects. You can see all the http headers for each request in your browser (If you are using chrome open developer tools, go to network, adjust the view and reload the page, you will see all requests made by your browser, if you click on any on any of these requests then click on headers you will see the headers of each request).

该函数header("Content-type:application/json")将 http json 标头发送到浏览器,以告知浏览器他期望的数据类型。您可以在浏览器中看到每个请求的所有 http 标头(如果您使用的是 chrome open 开发者工具,请转到网络,调整视图并重新加载页面,您将看到浏览器发出的所有请求,如果您点击任何在这些请求中的任何一个上,然后单击标头,您将看到每个请求的标头)。

When you use this function you will notice the http header Content-Type:application/jsonin the response sent from the server. If you don't use it the server will send the default which most likely is Content-type:text/html; charset=UTF-8

当您使用此函数时,您会注意到Content-Type:application/json服务器发送的响应中的 http 标头。如果您不使用它,服务器将发送最有可能的默认值Content-type:text/html; charset=UTF-8

As @Monty stated you don't need this function if you added dataType: 'json'to your AJAX as Jquery will handle the data even it is sent with text/html header.

正如@Monty 所说,如果您添加dataType: 'json'到 AJAX 中,则不需要此功能,因为 Jquery 将处理数据,即使它是使用 text/html 标头发送的。

See Also: jQuery AJAX Call to PHP Script with JSON Return

另请参阅:jQuery AJAX 调用 PHP 脚本并返回 JSON

To read more about headers : http-headers-for-dummies

要阅读有关标头的更多信息:http-headers-for-dummies