JavaScript可以在window.open上设置mime类型 [英] Can JavaScript set the mime type on window.open

查看:256
本文介绍了JavaScript可以在window.open上设置mime类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Javascript中打开调试窗口。 Javascript会将调试窗口传递给JSON字符串,该字符串 JSONView(Chrome扩展程序)应以格式良好的方式显示。

I am trying to open a debug window within Javascript. Javascript will pass the debug window a JSON string which JSONView (a Chrome extension) should display in a nicely formatted way.

为此,MIME类型必须为 application / json 。是否可以将mime类型和JSON字符串发送到 window.open 作为参数如何?我认为必须在window.open上设置MIME类型和内容,否则JSONView不会被触发。

For this to work the MIME type must be "application/json". Is it possible to send the mime type and JSON string to window.open as a parameter some how? I think the MIME type and content has to be set on window.open otherwise JSONView won't get triggered.

我试过这个,但是没有用:

I did try this, but it did not work:

var x = window.open("about:blank", 'x'); 
var doc = x.document; 
doc.open("application/json"); 
doc.write($(".trend_chart").attr("data-trendChart"))


推荐答案

您的开头应该是 application / json 类型的文件,您不能将其作为参数发送到 window.open 方法因为它脱离了上下文。浏览器是使用请求标头确定文件类型的浏览器。

The document that your opening should be of type "application/json" you cannot send it as a parameter in the window.open method since it's out of context. The browser instead is the one that determines the file type using the request headers.

window.open("http://www.yoursite.com/file.json", "mywindow");

您应该在JSONView中看到json文件没有问题。如果浏览器仍然要求您下载该文件,则JSONView的安装可能已损坏。

You should see the json file within JSONView without problems. If the browser still asks you to download the file, your installation of JSONView is probably broken.

这篇关于JavaScript可以在window.open上设置mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆