如果返回的数据是javascript,$.ajax会自动执行脚本吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如果返回的数据是javascript,$.ajax会自动执行脚本吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3137字,纯文字阅读大概需要5分钟。
内容图文
![如果返回的数据是javascript,$.ajax会自动执行脚本吗?](/upload/InfoBanner/zyjiaocheng/745/89a7f8811b344f9184d21a2439890931.jpg)
我使用一个JS文件将我的所有数据发布回我的服务器,使用:
$.ajax({
url: "/backend/post.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(response, status, xhr) // A function to be called if request succeeds
{
var ct = xhr.getResponseHeader("content-type") || "";
if(ct.indexOf("text/plain") > -1){
alert(response);
console.log('text - response');
}
if(ct.indexOf("text/javascript") > -1){
//eval(response);
console.log('javascript - response');
}
}
});
它经历了服务器端的一整套功能,但最终得到了这个:output_javascript(“alert(‘item added’);”);
function output_javascript($script)
{
header("content-type:text/javascript");
echo $script;
}
我们的想法是让$.ajax函数显示文本或从服务器执行脚本.
当$.ajax从output_javascript获取响应时(“alert(‘item added’);”);然后它执行两次代码.当我在成功函数中注释掉要执行的代码时:
$.ajax({
url: "/backend/post.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(response, status, xhr) // A function to be called if request succeeds
{
}
});
然后它只执行一次响应.让我相信$.ajax在返回响应变量中的脚本之前执行代码.
这是真的,还是我不能正确理解$.ajax?如果我误解了$.ajax函数,有人可以告诉我如何解决这个问题吗?
解决方法:
是的,ajax将执行返回的JavaScript代码.我们可以在the documentation看到这个:
dataType (default: Intelligent Guess (xml, json, script, or html))
Type: String
The type of data that you’re expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
因此,如果您没有指定dataType,jQuery将从响应中找出它.好的,但它对“脚本”值有什么作用?再向下:
“script”: Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, “_=[TIMESTAMP]”, to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.
后来在讨论中:
If
script
is specified,$.ajax()
will execute the JavaScript that is received from the server before passing it on to the success handler as a string.
只需在页面上搜索单词“JavaScript”,就可以在文档中轻松找到所有这些内容.
内容总结
以上是互联网集市为您收集整理的如果返回的数据是javascript,$.ajax会自动执行脚本吗?全部内容,希望文章能够帮你解决如果返回的数据是javascript,$.ajax会自动执行脚本吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。