【发布时间】:2018-08-31 19:18:17
【问题描述】:
最近我遇到了将 select2 与 laravel 集成的问题。
我想调用 ajax 请求来过滤用户,但没有找到任何结果。
我在本地主机中创建了测试文件:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
<select class="js-data-example-ajax"></select>
<script>
$('.js-data-example-ajax').select2({
ajax: {
url: 'ajax.php',
dataType: 'json'
// Additional AJAX parameters go here; see the end of this chapter for the full code of this example
}
});
</script>
Ajax.php
<?php echo "{
\"results\": [
{
\"id\": 1,
\"text\": \"Option 1\"
},
{
\"id\": 2,
\"text\": \"Option 2\"
}
]
}";
这运行正常,但是当我在我的 laravel 刀片模板中使用相同的脚本时,我得到 Nore sult found:
index.blade.php(注意:我在 head 部分加载了 jquery 和 select2)
<select class="js-data-example-ajax"></select>
<script>
$('.js-data-example-ajax').select2({
ajax: {
url: 'http://localhost/ajax.php',
dataType: 'json'
// Additional AJAX parameters go here; see the end of this chapter for the full code of this example
}
});
</script>
【问题讨论】:
-
你有解析
http://localhost/ajax.php的路由吗? -
不,我不知道。我刚刚让它工作了我将脚本添加到 /resources/assets/js/app.js 运行 npm dev 并且它的工作......
-
好吧,如果您要 ajaxing 的 url 不存在,则可能没有结果。
-
那个文件存在于那个路径上。没关系,我解决了它
标签: php jquery ajax laravel jquery-select2