ajaxでpostしてcontrolllerからモーダルを出そうとしているのですが出てこないので困っています。
realshopcontroller
public function addToCast($store_id, $coupon_id) { $store = Store::find($store_id); $coupon = $store ->coupons() ->where('status', COUPON_STATUS_RELEASE) ->where('type', COUPON_TYPE_NORMAL) ->where('coupons.id', $coupon_id) ->first(); if (is_null($coupon->used)) $coupon->used = 0; if ($coupon && ($coupon->sum === 0 || $coupon->used < $coupon->sum) && !$coupon->hasDownloaded($store->id, auth()->user()->id)) { auth()->user()->add_coupon([ 'store_id' => $store->id, 'coupon_id' => $coupon_id, ]); return response()->json([ 'code' => 0, 'error' => '', 'html' => view('coupon_ok', compact('store', 'coupon'))->render() ], 200); } return response()->json([ 'code' => 500, 'error' => 'maximum number of coupon collection', 'html' => view('coupon_error', compact('store'))->render() ], 200); }
web.php
Route::POST('/realshop/{store_id}/coupon/{coupon_id}/fetch', 'User\RealShopController@addToCast')->name('coupons.addtocast');
main.js
const download_coupon_click = function (ele, store_id, coupon_id) { ele.disabled = true; ele.innerText = "Loading..."; event.preventDefault(); $.ajaxSetup({ cache: false }); $.ajax({ 'url': '/realshop/' + store_id + '/coupon/' + coupon_id + '/fetch', 'type': 'POST', //data: $.param({store_id: store_id, coupon_id: coupon_id}) }).done(function(data) { // console.log(data); if (data.code === 401) { location.href = '/login'; } else { $("#ajaxModal").html(data.html); $('#ajaxModal').modal('show'); } }).fail(function (jqXHR, textStatus, errorThrown) { // 通信失敗時の処理 alert('ファイルの取得に失敗しました。'); console.log("ajax通信に失敗しました"); console.log("jqXHR : " + jqXHR.status); // HTTPステータスが取得 console.log("textStatus : " + textStatus); // タイムアウト、パースエラー console.log("errorThrown : " + errorThrown.message); // 例外情報 }); };
coupon_ok.blade
<div class="modal-dialog"> <div class="modal-content" style="background: transparent"> <div class="modal-body" style="text-align: center; background: white; border-top: 4px solid #ffa900"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> 以下略
coupon.blade.php
<div class="position-relative d-none d-md-block" > <span style="width:calc(100% + 20px); height:50px; position:absolute; left:-10px; top:-10px; border-top:2px solid #fb9d38; border-right:2px solid #fb9d38;" id="ajaxModal"></span> <button onclick="download_coupon_click(this, {{$store->id}}, {{ $coupon->id }})" class="btn font-weight-bold font-18" style="border-radius: 3px; height:150px; line-height:30px; white-space:initial; min-height:70px; width:100%; background:#fb8d45; display:block;" >このクーポンを<br>使う・予約する</button> <span style="width:calc(100% + 20px); height:50px; position:absolute; left:-10px; bottom:-10px; border-bottom:2px solid #fb9d38; border-left:2px solid #fb9d38;"></span> </div>
回答1件
あなたの回答
tips
プレビュー