【问题标题】:Jquery and laravel 404 not found找不到 Jquery 和 laravel 404
【发布时间】:2017-09-16 07:16:16
【问题描述】:

嘿,我是 laravel 的新手,我正在制作一个自动完成搜索栏。但是我遇到的问题是,当我写任何单词时,它会在我的控制台面板中给出错误

Failed to load resource: the server responded with a status of 404 (Not Found). jquery-1.10.2.js:8706

在 jquery 中出现此错误的行是

xhr.send( ( s.hasContent && s.data ) || null );

请帮助我。我已经尝试了很多,但没有得到所需的结果。

我在其中包含 jquery 的代码的视图是

  <html>
 <head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

</head>
 <body>
<div class="row">
    <div class="col-md-4"></div>
   <div class="col col-md-4">
      <section class="card">
         <header class="card-heading">
             <input type="text" name="searchname" class="form-control" id="searchname" placeholder="Search"  >
          </header>
          <div class="card-block">
            <table>
                <tr>
                  <td>ID</td>
                  <td><input type="text" name="id" class="form-control"></td>
                </tr>
                <tr>
                <td>Symtomps</td>
                  <td><input type="text" name="symtomps" class="form-control" ></td>
                </tr>
              </table>

          </div>
       </section>
    </div>
</div>
</body>
 <script type="text/javascript">
$('#searchname').autocomplete({
    source: '{!!URL::route('autocomplete')!!}',
    //source: '{{ asset('search') }}',
    //source: '{{URL::route('autocomplete')}',
    minlength:1,
    autoFocus:true,
    select:function(e,ui){
    //$('#id').val($data->symtomps);
}
});

</script>

我的代码的控制器是

   <?php

   namespace App\Http\Controllers;


   use Illuminate\Http\Request;
   use App\Disease;
   use Input;

   class DiseaseController extends Controller
    {
 public function index()
 {
     return view('disease.disease');
 }
public function autocomplete(Request $request)
{
   $term = $request->term;
    $data = Disease::where('symtomps','Like','%'.$term.'%')
    ->take(2)
    ->get();
    $result=array();

    foreach($data as $key => $v)
    {
        $results[]=['id' =>$v->id,'value'=>$v->symtomps];
    }
    return response()->json($results);
}

}

【问题讨论】:

  • 分享包含“jquery.js”的代码?
  • @user2486 分享了我的代码
  • 您确定在根目录下有文件夹名称code.jquery.com 并且那里有查询文件吗?
  • @M_Idrees 我用过jquery文件的cdn
  • 没有调试就很难提出建议。让我们试试 put echo for "{!! URL::route('autocomplete') !!}" 看看它是否返回正确的可访问路径,还用一些硬编码的数组项替换源属性,如var items = [ "item1", "item2" ]; 然后source:items, 并检查它是否以这种更简单的形式工作?跨度>

标签: php jquery laravel-5


【解决方案1】:

你没有资源(意味着没有任何像 json 这样的存储文件)使用 ajax 或这样做。

source: function (request, response) {
    $.get("{!!  URL::route('autocomplete')  !!}", {
        query: request.term
    }, function (data) {
        response(data);
    });
},

【讨论】:

  • 我应该更改哪些引号是这一行中的两个引号
  • 按我给定的方式改变
  • 仍然给出错误加载资源失败:服务器响应状态为 404(未找到)/TestProject/public/autocomplete?term=t
  • 你有路由autocomplete/autocomplete吗?
  • 是的,这是我的路线 Route::get('/autocomplete',array('as'=>'autocomplete','uses'=>'DiseaseController@autocomplete'));
【解决方案2】:

如果您使用来自 CDN 的 jQuery,那么您必须指定正确格式的完整链接。

尝试换行:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

【讨论】:

  • 我的控制台中仍然出现错误 /TestProject/public/autocomplete?query=t 加载资源失败:服务器响应状态为 404(未找到)
猜你喜欢
  • 2020-03-26
  • 2019-12-02
  • 2020-09-02
  • 2021-06-22
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2019-10-20
相关资源
最近更新 更多