是否可以在js< script>上设置自定义标头要求? [英] is it possible to set custom headers on js <script> requests?

查看:91
本文介绍了是否可以在js< script>上设置自定义标头要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样可能吗?

<script src="http://myserver.com/some.js" my-custom-header="foo"></script>

更新(更多细节):

有人问我是否有一种方法,可以使用标头而不是GET参数将某些参数作为脚本请求的一部分传达给服务器.我说:不,"但以为我会仔细检查.

I've been asked if there was a way to communicate some parameters to the server as part of the script request using headers instead of GET params. I said, "no," but thought I'd double check.

推荐答案

简短答案:否.默认情况下,脚本标记将仅检索src属性中指定的资源.

Short answer: no. By default a script tag will just retrieve the resource specified in the src attribute.

但是,如果您使用AJAX请求来检索脚本(并在以后添加/执行它),则可以使用XMLHttpRequest对象的setRequestHeader函数(请参见

However, if you use an AJAX request to retrieve the script (and add it later/execute it), you can use the setRequestHeader function of the XMLHttpRequest object (see http://www.developertutorials.com/learn-ajax/custom-http-headers-2643.php).

您还可以使用更复杂的方法,例如使用mod_rewrite重写路径,并将参数包括在url中.最佳解决方案取决于您要执行的操作以及对服务器的控制权.

You could also use more complex methods, such as using mod_rewrite to rewrite paths, and include the parameters in the url. The best solution depends on what you want to do, and how much control you have over the server.

这篇关于是否可以在js&lt; script&gt;上设置自定义标头要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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