简体   繁体   English

laravel使用json将数据从控制器传递到jquery

[英]laravel pass data from controller to jquery using json

I want to pass data from controller to jquery using json don't know where is the problem but fro the jquery code I think its working fine as I tested the success code but can't get back the result from controller 我想使用json将数据从控制器传递到jquery,不知道问题出在哪里,但是在jquery代码中,我认为它在我测试成功代码时可以正常工作,但无法从controller取回结果

home.blade 刀片

    <form role="form" name="form_address" id="form_address" action="" method="POST"  enctype="multipart/form-data">
     {{ csrf_field() }}
  <input type="text" id="postal_code" onFocus="geolocate()">
  <input type="text" id="totaldistance"  onFocus="geolocate()">
 </form>
  <button id="save_address">Save</button>
 <script>
$("#save_address").click(function (e) {
$.ajaxSetup({
    headers: {
         'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
    }
   });

   e.preventDefault();
  var form = document.forms.namedItem("form_address");
  var formData = new FormData(form); 
  $.ajax({
     type: "get",
    url: 'Get_distance',
    contentType: false,
    data: formData, 
    processData: false,
    success: function(data) {
      $('#totaldistance').val(data.distance); 
    }
   });
  });

web.php web.php

Route::post('Get_distance','HomeController@getdistance');

controller 控制者

public function getdistance(Request $request)
{
  $distance =$request->postal_code;

  return Response::json(array(
    'distance' => $distance,  
  ));
}

将您的ajax类型更改为POST ,因为您的路由类型是POST而不是GET

Your defined route in web.php is a POST request, but your Ajax method is set to GET request. 您在web.php中定义的路由是POST请求,但是您的Ajax方法设置为GET请求。 Change web.php to a GET request for it to work. 将web.php更改为GET请求以使其正常工作。 Make sure to provide an error function to catch any errors from server side. 确保提供错误功能以捕获服务器端的所有错误。

Or vice versa, change Ajax request to POST since you already added the csrf setup. 反之亦然,因为您已经添加了csrf设置,所以将Ajax请求更改为POST。

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

相关问题 在Laravel 5.4中使用JQuery Ajax从控制器传递数据以进行查看 - Pass data from controller to view using JQuery Ajax in Laravel 5.4 通过POST将JSON数据从AngularJS传递到Laravel Controller - Pass JSON data from AngularJS to Laravel Controller via POST Laravel:如何将数据从控制器传递到存储库 - Laravel: how to pass data from controller to repository 如何:在Laravel 4中将数据从控制器传递到JavaScript? - How to: pass data from controller to JavaScript in Laravel 4? 如何将JSON数据从控制器传递到JavaScript - How to pass json data from the controller to javascript 从jQuery Fullcalendar获取数据并将其传递给控制器 - Get data from jQuery Fullcalendar and pass it to controller 在路由时在不使用服务或Factory的情况下将JSON数据从控制器传递到另一个控制器 - Pass the JSON Data Between From Controller To another Controller with out using Services or Factory in angular JS while Routing 将数据从Laravel $ collection传递到jquery循环 - Pass data from Laravel $collection to jquery loop 使用JQuery将JSON数据传递到DIV中 - Using JQuery to pass JSON data into DIVs 在 Laravel 中,如何简单地将数据从 js 传递到 controller 返回查看? - In Laravel, how to simply pass data from js to controller back to view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM