weixin_33696822 2015-09-08 07:01 采纳率: 0%
浏览 46

阿贾克斯不工作

I have following code in my view

$(document).ready(function(){
     $("#mySelect").on('change',  function () {
        $.ajax({
          url: 'list-of-product-one',
          type: "get",
          data: {'id':$(this).val()},
          success: function(data){
          console.log(data);
        }
    });        

<select class="form-control"  id="mySelect" name="cat_id" >             
   @foreach($category_list as $data)
      <option value="{{$data->id}}">{{$data->cat_name}}</option>
   @endforeach 
</select>

and in my controller

public function ProductListGet(Request $request)
{
    $category_list=Category::all();
    $list_of_product=Product::where('cat_name','=',$request->id)->get();
    return view('manager/list_of_product',['list_of_product'=>$list_of_product,'category_list'=>$category_list]);
}

if I run that then it throws an error in my web browser console like

GET XHR http://localhost/poet/public/manager/list-of-product-one [HTTP/1.0 500 Internal Server Error 156ms]

so I changed return in my controller like

 return json_encode($list_of_product);

then it displays the json array in my console even if return without

 $list_of_product=Product::where('cat_name','=',$request->id)->get();

this condition then it will return whole page

now my question is suppose if I have condition in my controller then how can I return my view ?

  • 写回答

1条回答 默认 最新

  • weixin_33695082 2015-09-08 07:34
    关注

    GET XHR http://localhost/poet/public/manager/list-of-product-one [HTTP/1.0 500 Internal Server Error 156ms]

    Looks like you forgot send the _token. Add the following meta tag into the header section:

    <meta name="csrf-token" content="{{ csrf_token() }}">
    

    Now, send the token into the ajax request:

    $(document).ready(function(){
    
         // here you prepare the token
         $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    
         $("#mySelect").on('change',  function () {
            $.ajax({
              url: 'list-of-product-one',
              type: "get",
              data: {'id':$(this).val()},
              success: function(data){
              console.log(data);
         }
    }); 
    

    this condition then it will return whole page

    In other hand, you should check your view content because the use of @extends('layout') makes return the content of the entire template. Just put html's markup as simple without extending the template.

    评论

报告相同问题?

悬赏问题

  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了