简体   繁体   English

内容类型Ajax json丢失

[英]Content-Type Ajax json missing

I wrote some php code that outputs some valid json, and sets the content-type header to application/json in my dev setup. 我写了一些PHP代码,输出一些有效的json,并在我的开发设置中将content-type标头设置为application / json。 However when I deploy this script to a embedded webserver it works fine except it's not capable of sending the content-type. 但是,当我将此脚本部署到嵌入式Web服务器时,除了不能发送内容类型之外,它都能正常工作。 It's not possible to run a other webserver. 无法运行其他Web服务器。

Now I have the following code for Dynatable. 现在,我有以下用于Dynatable的代码。 Even though my dev and my embedded webserver, serve exactly the same file, and the only difference is the content-type. 即使我的开发人员和嵌入式Web服务器提供完全相同的文件,唯一的区别是内容类型。 It works for my dev setup, however it doesn't work for my embedded setup. 它适用于我的开发设置,但不适用于我的嵌入式设置。

I use the following code to load the json file to dynatable. 我使用以下代码将json文件加载到可动态生成的文件中。

document.ready(
    $.ajax({
        url: 'phpApi.php',
        success: function(data){
            $('#myTable').dynatable({
                dataset: {
                    records: data
                }
            });
        }
    }));

So can someone explain me why the content-type is so important for ajax? 那么有人可以解释一下为什么内容类型对ajax如此重要吗? How can I tell my code manually its json? 如何手动告诉我的代码其json?

Without the content-type the returned data is assumed to be plain text. 如果没有内容类型,则返回的数据假定为纯文本。 There is nothing in your code to tell it otherwise. 您的代码中没有其他内容可以告诉您。

One way to get json would be to specify the return type in the jquery code. 获取json的一种方法是在jquery代码中指定返回类型。 Just add dataType: 'json' into the ajax configuration. 只需将dataType: 'json'添加到ajax配置中。

Or you could use eval() to transform the returned text to json. 或者,您可以使用eval()将返回的文本转换为json。

document.ready(
    $.ajax({
        url: 'phpApi.php',
        success: function(data){
            $('#myTable').dynatable({
                dataset: {
                    records: eval(data)
                }
            });
        }
    }));

Using JSON.stringify(eval(data)) might give you better results by making sure its json. 使用JSON.stringify(eval(data))可以通过确保其json为您提供更好的结果。

As pointed out below, JSON.parse(data) would probably be safer. 如下所述, JSON.parse(data)可能会更安全。 (Eval is evil after all.) (毕竟,评估是邪恶的。)

So can someone explain me why the content-type is so important for ajax? 那么有人可以解释一下为什么内容类型对ajax如此重要吗?

It's important so the client can identify what type of content the server returned, content-type: application/json tells jQUery to parse the data as an object. 重要的是,客户端可以识别服务器返回的内容类型内容类型:application / json告诉jQUery将数据解析为对象。 If no content type is returned, the client will assume the returned data is just plain text. 如果未返回任何内容类型,则客户端将假定返回的数据只是纯文本。

How can I tell my code manually its json? 如何手动告诉我的代码其json?

Add dataType: "json" parameter to $.ajax() dataType:“ json”参数添加到$ .ajax()

document.ready(
    $.ajax({
        url: 'phpApi.php',
        dataType: "json",
        success: function(data){
            $('#myTable').dynatable({
                dataset: {
                    records: data
                }
            });
        }
    }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM