JSON file set content type to application/json AFNetworking

天涯浪子 提交于 2019-12-24 00:05:28

问题


i wrote a .json file and want to download it with AFNetworking. But AFNetworking complains: fail Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/plain

my JSON file test.json

{
"count-packages": 5,
"packages": 
{
    "de":
    {
        "0": "Wackel Dackel",
        "1": "Hans Wurst",
        "2": "Peter Ploes",
        "3": "Tiffel Toffel",
        "4": "China Mann"
    },
    "en":
    {
        "0": "Wobble dachshund",
        "1": "Hans Sausage",
        "2": "Peter Ploes",
        "3": "Tiffel Potato",
        "4": "Peking Ente"
    }
}

}

HTTP/1.1 200 OK
Date: Mon, 06 Feb 2012 17:31:06 GMT
Server: Apache/1.3.41 Ben-SSL/1.59
Last-Modified: Mon, 06 Feb 2012 17:28:13 GMT
ETag: "18039c71-205-4f300dad"
Accept-Ranges: bytes
Content-Length: 517
Content-Type: text/plain

How can I change the Content-Type?


回答1:


It is the issue on the server side. If you have control to the Apache server, add following line in httpd.conf

application/json            json

EDIT : I was wrong of the syntax with my answer above. The lines like above should go into mime.types file as @James suggests. But you can use AddType directive and specify mime types in httpd.conf as well.

AddType application/json .json



回答2:


You'll need to add an appropriate entry for .json to the Apache mime.types file. See Setting up MIME Types with Apache




回答3:


If you don't have access to httpd.conf (like in many cases) you can create a .htaccess file and simply write AddType application/json .json inside. Put it next your json file and it should be OK !




回答4:


You can't change the content type for a .json file. Probably the error comes from the javascript, ther you sould change or parse as Json



来源:https://stackoverflow.com/questions/9164486/json-file-set-content-type-to-application-json-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!