weixin_33713503 2017-08-08 18:55 采纳率: 0%
浏览 180

Ajax请求返回HTML页面

I am trying to send data from an Input to the controller of my application in Laravel, where I supply the category ID and it returns a jSON with the related id's.

I have a input to get data

<label for="categoryMae">Nome da Categoria Mãe</label>
      <input class="form-control" id="getCategoryMae" name="getCategoryMae" />

A script to pass to my controller

<script>
$("#getCategoryMae").blur(function(){
  var cat_id = $(this).val();
  console.log(cat_id);
  $.ajax({
    type: 'GET',
    url: '{{route('getCategoryName')}}',
    data: 'cat_id',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
      console.log(data.cat_id);
    }
  });

});
</script>

and I trying to get this values in my Controller (Route::get('/categories/getCategoryName', 'CategoriesController@getCategoryName')->name('getCategoryName');)

public function getCategoryName(Request $request)
    {
       $data = $request->all();
       dd($data);
    }

But the code returns the HTML structure and not the the value

enter image description here

How return only the value and not all that structure and why that happened? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • derek5. 2017-08-08 19:38
    关注

    I don't think you're sending any data to your controller. Try this, check the data property of the AJAX call.

    <script>
    $("#getCategoryMae").blur(function(){
      var cat_id = $(this).val();
      console.log(cat_id);
    
      var dataObject = {cat_id: cat_id};
      $.ajax({
        type: 'GET',
        url: '{{route('getCategoryName')}}',
        data: dataObject,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){
          console.log(data.cat_id);
        }
      });
    
    });
    </script>
    

    Also, I can tell you use Google Chrome, you can make sure you send something to the server, you can click on the Headers, go to the bottom to the Query String Prameters to see the parameters you're sending. Here's a good resource: Chrome Dev Tools

    评论

    报告相同问题?

    悬赏问题

    • ¥15 matlab处理脑电数据悬赏(时序图+预处理+频谱图)
    • ¥100 r语言多元回归模型怎么看表达式
    • ¥15 PMM 9010/30P
    • ¥15 pom文件依赖管理,未找到依赖
    • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
    • ¥15 Unity导出2D项目运行时图片变成马赛克
    • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
    • ¥15 matlab中此类型的变量不支持使用点进行索引
    • ¥15 咨询第六届工业互联网数据创新大赛原始数据
    • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!