【问题标题】:Laravel select2 no results foundLaravel select2 没有找到结果
【发布时间】: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


【解决方案1】:

如果您只想在 laravel 项目中运行您的示例,您应该将 Ajax.php 文件移动到您的公共文件夹中。 如果您希望您的列表是动态的而不仅仅是静态 json 内容,您应该为将生成 select2 元素的函数创建一个路由

web.php

Route::get('select2list', 'TestController@select2list');

TestController.php

public function select2list()
{
    //generate your list in the way you want
    $temp = '{
      "results": [
           {
              "id": 1,
              "text": "Option 1"
           },
           {
              "id": 2,
              "text": "Option 2"
           }
         ]
        }';
    die(json_decode( $temp ));
}

要在你的视图中加载这个列表,你应该像这样发出这个 ajax 请求

var mySelect2Elements;
$.ajax({
    url: "/select2list",
    dataType: 'json',
    type: "GET",
    success: function (data) {

         alert( data );
         mySelect2Elements = data;

    }
}).done(function() {
    $('.js-data-example-ajax').select2(
       { data: mySelect2Elements}
    );
});

【讨论】:

  • 问题在于 javascript 而不是应用程序逻辑。当我将它移动到 app.js 并构建它时,我在刀片模板中有 javascript 并使其正常工作
  • 如果有任何错误,请在评论中留下错误信息,否则如果我的回复回答了您的问题,请接受它
  • 不,不是。正如我所说,问题出在 javascript
  • 你能打开控制台告诉我到底是什么错误吗?
  • 控制台没有错误。我正在使用 laravel webpack,当我将该函数包含到 resources/assets/js/app.js 并运行 npm dev 时,它正在工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2021-07-08
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多