简体   繁体   English

无法在 laravel 中发送 ajax

[英]can not send post the ajax in laravel

I'm try to add click get value from my table this is my table我正在尝试从我的表中添加点击获取值这是我的表

@foreach($inventory as $p)
                  <?php 
                    $no++;

                    $typesql = DB::table('product_variant_pos')
                        ->where('id_product',$p->id)
                        ->select('type')
                        ->distinct()
                        ->get();
                  ?>
                    <tr >
                      <th scope="row"><?php echo $no;?></th>
                      <td style="width:100px;">
                        @if($p->featured_image != null || $p->featured_image != '')         
                          <img src="{{ 'Images/'.$p->featured_image }}" alt="{{ $p->featured_image }}" title="{{ $p->featured_image }}" width="100px">
                        @else
                          <h4>Nothing Image Upload</h4>
                        @endif                          
                      </td>
                      <td class="data-product" id="{{ $p->id }}" data-toggle="modal" data-target="#ModalProduct" >{{ $p->item_name }}</td>
                      <td>
                        @if(!is_null($typesql))
                            @foreach($typesql as $t)
                            <ul>
                            <?php 
                            $type = DB::table('product_variant_pos')
                                ->where('id_product',$p->id)
                                ->where('type',$t->type)
                                ->get();
                            ?>
                                <li>{{ $t->type }}
                                    <ul>
                                    @foreach($type as $j)
                                        <li>{{ $j->name }}</li>
                                    @endforeach
                                    </ul>
                                </li>
                            </ul>
                            @endforeach
                        @else
                        <?php 
                            $type = DB::table('product_variant_pos')
                                ->where('id_product',$p->id)
                                ->get();
                            ?>
                                <li>{{ $t->type }}
                                    <ul>
                                    @foreach($type as $j)
                                        <li>{{ $j->name }}</li>
                                    @endforeach
                                    </ul>
                                </li>
                            </ul>
                        @endif
                      </td>
                      <td>{{ $p->category }}</td>
                      <td>{{ $p->stock }}</td>
                      <td>Rp. {{ $p->price }}</td>
                      <td>
                        <a href="product/edited/{{ $p->id }}">Edit</a>
                        |
                        <a href="product/delete/{{ $p->id }}">delete</a>
                      </td>
                    </tr>
                  @endforeach

and this is my javascript这是我的javascript

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
    
    
    $('.data-product').click(function(e){
        
        var formData = {
            id: $(this).attr("id"),
        };
        
        var button_id = $(this).attr("id");
        
        $.ajax({
           type:'POST',
           url:'product/modal',
           data: formData,
           headers: {
                  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
           },
           success:function(data) {
              $("#ModalProductTitle").html(data);
           },
           error: function (data) {
              console.log(data);
           }
        }); 
    });
    
} );
</script>

this script make value这个脚本创造价值

The post method is not supported for this route.此路由不支持 post 方法。

I am try code for other similar question but its not work.我正在尝试其他类似问题的代码,但它不起作用。 I am try in latest laravel and php .我正在尝试最新的laravelphp I think is caused csrf() is not detected in my code, but I don't know where is my mistake我认为是因为我的代码中没有检测到csrf() ,但我不知道我的错误在哪里

can anyone help me solve this problem, thankyou+.谁能帮我解决这个问题,谢谢+。

There are 2 alternatives, please choose one有2个选择,请选择一个

  1. Change your route type to POST because your AJAX type is POST Route::post('/product/modal', 'ProductController@Modal');将您的路线类型更改为 POST,因为您的 AJAX 类型是 POST Route::post('/product/modal', 'ProductController@Modal');
  2. Or change your AJAX type to GET because your route's type is GET或者将您的 AJAX 类型更改为 GET,因为您的路线类型是 GET

 $.ajax({ type:'GET', url:'/product/modal', data: formData, headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, success:function(data) { $("#ModalProductTitle").html(data); }, error: function (data) { console.log(data); } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM