Chrome Extension error when returning json: Resource interpreted as Script but transferred with MIME type text/json

天大地大妈咪最大 提交于 2019-12-25 02:24:20

问题


I am making a chrome extension that uses http://loopj.com/jquery-tokeninput/ to add tokens.

$(function() {
  $("#token").tokenInput("http://localhost/token/search", {
    preventDuplicates: true,
  });
});

In my php code I am returning a json encoded array (and the same function is used via the app itself and is working):

echo json_encode($token_array);
exit;

However the results are not being returned correctly and chrome reports the following error in the console:

Resource interpreted as Script but transferred with MIME type text/html

When I click the link the the source, it appears to be formatted correctly:

[
    {
        "id": "5",
        "name": "token1"
    },
    {
        "id": "3",
        "name": "token2"
    },
    {
        "id": "4",
        "name": "token3"
    }
]

Do I need to set headers in my php code? I tried:

header('Content-type: text/json');

which creates the following error:

Resource interpreted as Script but transferred with MIME type text/json:

and when I try

header('Content-type: application/json');

It doesn't seem to make the request. What am I doing wrong?


回答1:


Should be text/javascript content type:

header('content-type:text/javascript');



来源:https://stackoverflow.com/questions/13541612/chrome-extension-error-when-returning-json-resource-interpreted-as-script-but-t

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