javascript js 警告:资源被解释为脚本但使用 MIME 类型文本/纯文本传输

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7338983/
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-10-25 23:46:26  来源:igfitidea点击:

js warning: Resource interpreted as Script but transferred with MIME type text/plain

javascriptjquerywarningsjsonpcloudant

提问by tom81

i′m firing an ajax/jsonp request to my cloudant app:

我正在向我的 Cloudant 应用程序发出 ajax/jsonp 请求:

    var obj = $.ajax({
        url: "http://xyz",
        dataType: 'jsonp',
        success: function(data) {  
            //SOME CODE  
        },
        error: function() {
            //SOME CODE
        }
    });

the response is ok and i can read out my data. but I′m getting the following js warning:

响应正常,我可以读出我的数据。但我收到以下 js 警告:

Resource interpreted as Script but transferred with MIME type text/plain.

i need to make this request an jsonp request (cross domain policy), AFAIK jsonp returns as a script and gets executed by the browser. do i have to set a request header? I tried it with the 'accepts' and 'converters' options, but didn't make it work yet. (I′m using GoogleChrome, but also happens in Safari/FF)

我需要将此请求设为 jsonp 请求(跨域策略),AFAIK jsonp 作为脚本返回并由浏览器执行。我必须设置请求标头吗?我尝试了“接受”和“转换器”选项,但还没有让它工作。(我使用的是 GoogleChrome,但也发生在 Safari/FF)

cheers, tom

干杯,汤姆

PS: I want to get rid of the warning as this ajax request gets triggered every 2 seconds. So the console looks pretty bad...

PS:我想摆脱警告,因为此 ajax 请求每 2 秒触发一次。所以控制台看起来很糟糕......

回答by Jacob

The server should send a Content-Typeheader set to text/javascriptwhen it sends the JSONP script.

服务器应该在发送 JSONP 脚本时发送一个Content-Type设置为的标头text/javascript

回答by Robert Newson

CouchDB itself is sending the text/plain content-type. The only other type you can convince it to send is 'application/json' if you send 'Accept: application/json' as a header.

CouchDB 本身正在发送文本/纯内容类型。如果您将“接受:应用程序/json”作为标题发送,您可以说服它发送的唯一其他类型是“应用程序/json”。

It seems that CouchDB should send text/javascript if delivering a jsonp response, though. If you could file a ticket I'm sure it would get done.

不过,如果提供 jsonp 响应,CouchDB 似乎应该发送文本/javascript。如果你能提交一张票,我相信它会完成。

回答by Steve Benner

Are you sending any encoding information in the headers? I had a problem with CouchDB responses and discovered that adding charset=utf-8to my Acceptor Content-Typeheading resulted in CouchDB returning the text/plaincontent instead of the application/jsonI was expecting. Perhaps you can solve this by altering your header if the case is similar enough.

您是否在标头中发送任何编码信息?我遇到了 CouchDB 响应的问题,发现添加charset=utf-8到我的AcceptContent-Type标题会导致 CouchDB 返回text/plain内容而不是application/json我期望的内容。如果情况足够相似,也许您可​​以通过更改标题来解决此问题。

回答by genesis

Problem is on target server, because it's setting Content-Type: Text/plain

问题出在目标服​​务器上,因为它正在设置 Content-Type: Text/plain

You can't enforce it without access to target server. It should be Content-Type: text/javascript

您无法在无法访问目标服务器的情况下强制执行它。它应该是Content-Type: text/javascript