我正在使用Laravel应用程序,我的刀片文件中有一些数据,我想通过AJAX发布到后端。在下面的AJAX代码中,输入变量携带一个JSON对象(从PHP转换),而其他变量包含我从表单携带的数据。问题是当我单击“提交”按钮并将结果记录在控制台选项卡中时,我得到一个
的错误无法加载资源:服务器的响应状态为500(内部服务器错误)。
表单布局
@if (!empty($plans_benefits))
<div class="container">
<div class="PLAN">
<main class="top">
<div class="row">
@foreach ($plans_benefits as $plan_benefits)
@php
$plan_data = $plan_benefits[0];
$benefits = $plan_benefits[1];
// dd($benefits);
$plan_name = $plan_data->Calculation_TravelPlan->TravelPlan->Name;
@endphp
<div class="card plan">
<h5 class="card-title plan"> {{$plan_name}} </h5>
<img class="card-img-top plan" src="{{asset('assets/images-new/superior.svg')}}" alt="Card image cap">
<div class="card-body">
<div class="travel-plan">
<div class="superior-content">
<table class="table">
<tbody>
@foreach($benefits as $benefit)
<tr>
<td class="plan-title">{{$benefit->name}}</td>
@if($benefit->value == 'true')
<td class="plan-worth"><i class="fas fa-check"></i></td>
@elseif ($benefit->value == 'false')
<td class="plan-worth"><i class="fas"></i></td>
@else
<td class="plan-worth"> {{$benefit->value}} </td>
@endif
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<!-- Hiden-->
<input type="hidden" value="{{$plan_data->CalculationId}}"" class ="calc_id" name="calc_id" id="calc_id{{$plan_data->CalculationId}}"/>
<input type="hidden" value="{{$plan_name}}" class ="travelplan" name="travelplan" id="plan{{$plan_data->CalculationId}}"/>
<!--Hidden-->
<p class="card-text plan">TOTAL
<span class="amount">$ {{round($plan_data->TravelBasicPremium,2)}}
</span>
</p>
<!-- AJAX call (public/Js/App.js) line 31936-->
<a id ="{{$plan_data->CalculationId}}" class="plan-quote get_quote" style="cursor:pointer;"><span>Get Quote</span></a>
</div>
</div>
@endforeach
</div>
</main>
</div>
</div>
@endif
AJAX代码可通过发布请求将上述表单中的数据发布到Laravel后端
$('.PLAN').on('click', '.plan-quote', function () {
//Inputs variable below carry a JSON object converted from PHP
var inputs = {!!$form!!};
// console.log(inputs);
var calc_id = $(this).attr('id');
var c_id = $('#calc_id' + calc_id).val();
//console.log(c_id);
var plan_name = $('#plan' + calc_id).val();
// console.log(plan_name);
$.ajax({
//URL from routes file
url: 'getquote',
//POST request
type: 'post',
contentType: 'application/json',
data: inputs + '&calc_id=' + c_id + '&travelplan=' + plan_name,
success: function success(response) {
console.log(response);
},
error: function error(data) {
console.log(data);
}
});
//END AJAX REQUEST
});
路由文件
Route::post( '/getquote', 'B2CController@createQuote')->name('b2c.getquote');
用于处理发布请求的Laravel控制器
public
function createQuote(Request $request)
{
dd($request->all());
$data = [
'DobPrincipalTraveller' => $request->dob,
'TravelStartDate' => $request->departure_date,
'TravelEndDate' => $request->return_date,
'CoverOption' => $request->cover,
'WithSpouse' => 0,
'FirstName' => $request->FirstName,
'MiddleName' => $request->MiddleName,
'LastName' => $request->LastName,
'ClientEmail' => $request->email,
'ContactNumber' => $request->phone,
'CalculationId' => $request->calc_id,
'TravelPlan' => $request->travelplan,
'Children' => $request->childdob,
];
/*Posts data to an API via Curl*/
$quote = $this->global_Curl(
$data, 'api/travel/create-quote')->data;
dd($quote);
}
答案 0 :(得分:0)
当您在laravel中通过post方法使用ajax时,必须发送“ X-CSRF-TOKEN” 根据标题请求!例如:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
});
$.ajax({
type: 'post', // POST Request
url: 'url', // Url of the Route (in this case user/save not only save)
data: form_data, // Serialized Data
dataType: 'json', // Data Type of the Transmit
processData: false,
contentType: false,
success: function (data) {
},
error: function (data) {
}
});