从 PHP 文件加载 HTML 时,jQuery.load 或 jQuery.ajax() 哪个更快?
我有一个名为“content.php”的 PHP 文件,为这两种请求准备好了,作为一个例子:
<?php
// Id params passed via GET method
$get = $_GET['param'];
switch ($get) {
case "param_value":
?>
<div data-param="<?php echo $get; ?>">
// My HTML content here
</div>
<?php
break;
case default:
break;
}
// Id params passed via POST method
$post = $_POST['param'];
if ($post != "") {
$data['output'] = '
<div data-param="<?php echo $get; ?>">
// My HTML content here
</div>
';
echo json_encode($data);
}
?>
比我有 Javascript 文件,我从该文件对 PHP 进行 AJAX 调用:
var oWrapper = jQuery("#wrapper"),
// Loading HTML via jQuery.load() function
sParams = jQuery.param({ param: "value" });
oWrapper.load("/content.php?" + sParams, function () {
console.log("content loaded via load()");
});
// Loading HTML via jQuery.ajax() function
jQuery.ajax({
type: "POST",
dataType: "json",
url: "/content.php",
cache: false,
data: { "param": "value" },
success: function (data) {
oWrapper.html(data.output);
console.log("content loaded via ajax()");
}
});
哪种方式更快?
除了请求和返回的速度之外,我想知道应用程序哪种方式的安全性更好?!
I have PHP file named "content.php" prepared for both kind of requests, just as an example:
<?php
// Id params passed via GET method
$get = $_GET['param'];
switch ($get) {
case "param_value":
?>
<div data-param="<?php echo $get; ?>">
// My HTML content here
</div>
<?php
break;
case default:
break;
}
// Id params passed via POST method
$post = $_POST['param'];
if ($post != "") {
$data['output'] = '
<div data-param="<?php echo $get; ?>">
// My HTML content here
</div>
';
echo json_encode($data);
}
?>
And than I have Javascript file, from which I am making an AJAX call to PHP:
var oWrapper = jQuery("#wrapper"),
// Loading HTML via jQuery.load() function
sParams = jQuery.param({ param: "value" });
oWrapper.load("/content.php?" + sParams, function () {
console.log("content loaded via load()");
});
// Loading HTML via jQuery.ajax() function
jQuery.ajax({
type: "POST",
dataType: "json",
url: "/content.php",
cache: false,
data: { "param": "value" },
success: function (data) {
oWrapper.html(data.output);
console.log("content loaded via ajax()");
}
});
Which way is faster?
Besides the speed of requests and returns, I wish to know which way is better for security of the app?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两种方法都做几乎完全相同的事情。
在内部,
$(selector).load()
使用$.ajax()
获取数据,然后$(selector).html()
> 将所选元素的 html 设置为$.ajax()
调用的响应。如果要将 html 加载到元素中,请使用
$(selector).load()
因为它更具可读性。其中一种与另一种一样安全、快速。注意: jQuery 现在内部使用
$.parseHTML()
而不是$(selector).html()
将字符串转换为 html。但这并没有真正改变任何事情。Both ways do pretty much exactly the same thing.
Internally,
$(selector).load()
uses$.ajax()
to get the data, then$(selector).html()
to set the html of the selected element to the response of the$.ajax()
call.If you are loading html into an element, use
$(selector).load()
because it is more readable. One is just as secure and as fast as the other.Note: Internally jQuery now uses
$.parseHTML()
rather than$(selector).html()
to convert the string to html. This doesn't really change anything though.我不知道具体细节,但我认为安全性没有区别。 load() 和 get() 是 ajax() 的“优化”版本,因此它们总是更快更好,但我不认为您或您的访问者会感受到像您这样的查询的差异
I don't know the specifics but I dont't think there is a difference in security. load() and get() are "optimized" versions of ajax() so they will always be faster and better but I don't you or your visitors will ever feel the difference in a query like yours