您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-请求-判断是否是pjax请求(及pjax的应用实例)直接加载整个页面是有问题的
发布时间:2022-01-18 22:14:51编辑:雪饮阅读()
后端实现:
<?php
namespace app\controller;
use support\Request;
class User
{
public function hello(Request $request)
{
$arr["Pjax"]=$request->isPjax();
var_dump($arr);
return response("<div>你好</div>");
}
}
后端部署并有前端请求时:
[root@localhost webman]# /www/server/php/73/bin/php -c /www/server/php/73/etc/php.ini start.php start
Workerman[start.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.26 PHP version:7.3.31
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root webman http://0.0.0.0:8787 8 [OK]
tcp root monitor none 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
array(1) {
["Pjax"]=>
bool(true)
}
array(1) {
["Pjax"]=>
bool(false)
}
array(1) {
["Pjax"]=>
bool(true)
}
array(1) {
["Pjax"]=>
bool(true)
}
array(1) {
["Pjax"]=>
bool(true)
}
前端实现:(注意同一域名下,简化处理跨域,否则需要单独处理跨域)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.pjax/2.0.1/jquery.pjax.js"></script>
<script>
$(document).ready(function(){
$("a[class=pjax]").click(function(event){
event.preventDefault();
var href=$(this).prop("href");
$.pjax({ url: href, container: 'div' });
});
});
</script>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a class="pjax" href="/user/hello">click me(use pjax)</a>
<a class="jump" href="/user/hello">click me(use traditional)</a>
<div></div>
</body>
</html>
访问百度
/www/server/php/73/bin/php -c /www/server/php/73/etc/php.ini /usr/bin/composer require guzzlehttp/guzzle 7.4.1
直接加载整个页面是有问题的
可能与页面中有其它js脚本导致的特别是对history有操作的吧
像是這樣<html>1</html>也不行,貌似與直接包含html這個標簽有關
对应请求头
return (bool)$this->header('X-PJAX');
关键字词:pjax,webman