Django的。如何发送'假'MIME类型图像以避免警告

时间:2021-02-18 09:34:33

I send a quick message to my server by setting the message as an image src. (It's faster than Ajax and I don't need a response).

我通过将消息设置为图像src来向我的服务器发送快速消息。 (它比Ajax更快,我不需要响应)。

var img = new Image();
var msg= "something happened";
img.src = my_domain_url +'?' + msg;

Server side I return something to avoid an error:

服务器端我返回一些东西以避免错误:

return HttpResponse("ok") 

However, I get a warning:

但是,我得到一个警告:

"Resource interpreted as Image but transferred with MIME type text/html:"

“资源解释为图像,但使用MIME类型text / html传输:”

How can I avoid that? I guess I need to send an image type, but do I need to send an actual image?

我怎么能避免这种情况?我想我需要发送图像类型,但是我需要发送实际图像吗?

1 个解决方案

#1


1  

Well of course you're going to get that message, you're telling JavaScript to get an Image at some URL but instead text is being returned. You could just do

那么你当然会得到那条消息,你告诉JavaScript在某个URL上获取一个图像,但是正在返回文本。你可以这样做

HttpResponse("ok", content_type="image/jpeg")

Though i'm not sure what the implications are of returning the string "ok" to a var image in JavaScript.

虽然我不确定将字符串“ok”返回到JavaScript中的var图像有什么影响。

see the documentation on the HttpResponse

请参阅HttpResponse上的文档

#1


1  

Well of course you're going to get that message, you're telling JavaScript to get an Image at some URL but instead text is being returned. You could just do

那么你当然会得到那条消息,你告诉JavaScript在某个URL上获取一个图像,但是正在返回文本。你可以这样做

HttpResponse("ok", content_type="image/jpeg")

Though i'm not sure what the implications are of returning the string "ok" to a var image in JavaScript.

虽然我不确定将字符串“ok”返回到JavaScript中的var图像有什么影响。

see the documentation on the HttpResponse

请参阅HttpResponse上的文档