jQuery是否会删除AJAX请求返回的脚本标签? [英] Does jQuery remove script tags returned by AJAX requests?

查看:99
本文介绍了jQuery是否会删除AJAX请求返回的脚本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于jQuery的JavaScript代码中执行AJAX调用.响应包含<script>标记.但是,似乎jQuery删除了此标记.

I'm performing an AJAX call in my jQuery-based JavaScript code. The response contains a <script> tag. However, it seems jQuery removes this tag.

这是jQuery或XHR的正常行为还是错误?

Is this normal behavior of jQuery or XHR or a bug?

推荐答案

这取决于您在AJAX调用中设置的dataType选项.如果将其设置为html,它将以纯文本的形式返回HTML,并带有准备插入DOM的脚本标签.如果将其设置为script,则jQuery将尝试运行脚本并将结果作为纯文本字符串返回.

It will depend on the dataType option you set in your AJAX call. If you set it to html it will give you back the HTML as plain text with the script tags ready for insertion to the DOM. If it is set to script then jQuery will attempt to run the script and return the result as a plain text string.

如果您未设置dataType,则jQuery将自动从响应MIME类型中推断出该类型.

If you are not setting a dataType then jQuery will automatically infer the type from the response MIME type.

看看jQuery文档( http://api.jquery.com/jQuery.ajax/ )以获取有关dataType设置的更多信息.

Have a look at the jQuery docs ( http://api.jquery.com/jQuery.ajax/ ) for more info on the dataType setting.

这篇关于jQuery是否会删除AJAX请求返回的脚本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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