参数未发送到 Ajax 中的 Laravel 路由
Parameter is not sent to Laravel route in Ajax
我正在与 Laravel 5.8 合作,我想为我的网站应用折扣代码系统。
所以我尝试像这样用 Ajax 发送数据:
$.ajax({
type: 'POST',
url: baseurl + 'discount/register',
data: {
coupon_code: finalDiscountValue,
course_id: itemCourse,
},
dataType: "json",
success: function (data) {
if(data == 99) {
swal("Wrong discount code");
} else if (data == 130) {
window.location.href = {!! json_encode(route('myCourseList')) !!}
} else{
window.location.href = {!! json_encode(route('payCourseRegistrationWithDiscount', ['course'=>$item->cor_id,'value'=>$data['cartValueDiscountedWithCode'] ?? ''])) !!}
}
}
});
现在我的问题是 $data['cartValueDiscountedWithCode']
没有发送到定义的路由名称。
这是因为 $data
在 Ajax 中被 return 编辑并且它的类型不同于另一个 $item->cor_id
和 Laravel 的变量基于变量。
这是我在控制器中 return $data
的方式:
public function registerWithDiscount()
{
$courseId = request('course_id');
$findCourse = Course::find($courseId);
$getCoursePrice = $findCourse->cor_price;
if ( request('course_id') == null ) {
return 97; // The input was empty
}
...
$data['cartValueDiscountedWithCode'] = $getCoursePrice - $value_discounted;
$data['discountedPrice'] = $value_discounted;
$data['coupon_id'] = $coupon->id;
$data['coupon_name'] = $coupon->name;
if($data['discountedPrice'] >= $getCoursePrice){
$this->registerDiscountFree($courseId,$data['cartValueDiscountedWithCode']);
session()->flash('registrationMessage', 'Your registration is completed');
return 130; // if the discount code makes the course free
}else{
return $data; // if the remainer balance still needs to pay
}
}
你可以试试这个:
let url = "{{ route('payCourseRegistrationWithDiscount',['course'=>$item->cor_id,'value'=>':cartValueDiscountedWithCode']) }}";
url = url.replace(':cartValueDiscountedWithCode', data.cartValueDiscountedWithCode);
window.location.href = url;
您不能像那样传递 ajax 响应值。
相反,在路由内添加一个字符串,然后用响应值
替换
您需要做的是将参数添加为可选参数 {value?}
example
然后添加 blade
window.location.href = {{route('payCourseRegistrationWithDiscount', ['course'=>$item->cor_id]) }}}+'/'+data.cartValueDiscountedWithCode
我正在与 Laravel 5.8 合作,我想为我的网站应用折扣代码系统。
所以我尝试像这样用 Ajax 发送数据:
$.ajax({
type: 'POST',
url: baseurl + 'discount/register',
data: {
coupon_code: finalDiscountValue,
course_id: itemCourse,
},
dataType: "json",
success: function (data) {
if(data == 99) {
swal("Wrong discount code");
} else if (data == 130) {
window.location.href = {!! json_encode(route('myCourseList')) !!}
} else{
window.location.href = {!! json_encode(route('payCourseRegistrationWithDiscount', ['course'=>$item->cor_id,'value'=>$data['cartValueDiscountedWithCode'] ?? ''])) !!}
}
}
});
现在我的问题是 $data['cartValueDiscountedWithCode']
没有发送到定义的路由名称。
这是因为 $data
在 Ajax 中被 return 编辑并且它的类型不同于另一个 $item->cor_id
和 Laravel 的变量基于变量。
这是我在控制器中 return $data
的方式:
public function registerWithDiscount()
{
$courseId = request('course_id');
$findCourse = Course::find($courseId);
$getCoursePrice = $findCourse->cor_price;
if ( request('course_id') == null ) {
return 97; // The input was empty
}
...
$data['cartValueDiscountedWithCode'] = $getCoursePrice - $value_discounted;
$data['discountedPrice'] = $value_discounted;
$data['coupon_id'] = $coupon->id;
$data['coupon_name'] = $coupon->name;
if($data['discountedPrice'] >= $getCoursePrice){
$this->registerDiscountFree($courseId,$data['cartValueDiscountedWithCode']);
session()->flash('registrationMessage', 'Your registration is completed');
return 130; // if the discount code makes the course free
}else{
return $data; // if the remainer balance still needs to pay
}
}
你可以试试这个:
let url = "{{ route('payCourseRegistrationWithDiscount',['course'=>$item->cor_id,'value'=>':cartValueDiscountedWithCode']) }}";
url = url.replace(':cartValueDiscountedWithCode', data.cartValueDiscountedWithCode);
window.location.href = url;
您不能像那样传递 ajax 响应值。 相反,在路由内添加一个字符串,然后用响应值
替换您需要做的是将参数添加为可选参数 {value?}
example
然后添加 blade
window.location.href = {{route('payCourseRegistrationWithDiscount', ['course'=>$item->cor_id]) }}}+'/'+data.cartValueDiscountedWithCode