我想將資料插入資料庫,但在控制器中是$request->ajax()錯誤的。有誰能夠幫我?我在谷歌上花了一整天,但沒有任何幫助。我正在使用 laravel 8。我將 ajax 用于過濾書。它作業正常。但是在這里我不知道錯誤在哪里。當我使用dd($request)之前,如果結果是:
這是.js檔案:
$(document).ready(function () {
$("#review-form").submit(function (e) {
e.preventDefault();
let reviewText = $("#reviewText").val();
let bookID = $("#bookID").val();
let userID = $("#userID").val();
$.ajax({
type: "POST",
url: "catalog/singleBook/" bookID,
dataType: "json",
data: {
reviewText: reviewText,
bookID: bookID,
userID: userID,
_token: $('meta[name="csrf-token"]').attr("content"),
},
success: function (response) {
alert("X");
console.log("X");
$("#review-container").prepend(
"<div id='review'>" response.reviewText "</div>"
);
$("#review-form")[0].reset();
},
error: function (error) {
console.log(error);
console.log("RREEER");
},
});
});
});
控制器:
public function storeReview(Request $request) {
dd($request->ajax());
if($request->ajax()) {
$review = new Review;
$review->bookID = $request->bookID;
$review->userID = $request->userID;
$review->reviewText = $request->reviewArea;
$review->save();
return response()->json($review);
}
}
看法:
@extends('layouts.main')
@section('title', 'Detail')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="row">
<div class="singleBook-container">
<div class="col-sm-2">
<img class="singleBook-img" src="{{ $book->img }}" alt="$request->ajax 在插入時為假">
</div>
<div hljs-number">3">
<ul hljs-keyword">list">
<li>
<div hljs-string">"> Nazov:</div>
<p>{{ $book->title }}</p>
</li>
<li>
<div hljs-string">"> Autor: </div>
<p>{{ $book->firstname }} {{ $book->lastname }}</p>
</li>
<li>
<div hljs-string">"> Rating:</div>
<p>{{ $book->rating }}</p>
</li>
<li>
<div hljs-string">"> ISBN: </div>
<p>{{ $book->ISBN }}</p>
</li>
</ul>
</div>
<div hljs-number">7">
<p>{{ $book->description }}</p>
</div>
</div>
</div>
<div hljs-string">">
<form id="review-form" method="Post">
@csrf
<div hljs-string">">
<input type="hidden" value="{{ $book->id }}" id="bookID" name="bookID" readonly />
<input type="hidden" value="{{ auth()->user()->id }}" id="userID" name="userID" readonly>
<textarea hljs-string">" name="reviewArea" id="reviewText exampleFormControlTextarea1"
rows="3" placeholder="Napí? recenziu.." required></textarea>
</div>
<button id="submitBTN" type="submit" hljs-string">">Odosla?</button>
</form>
</div>
<div hljs-string">">
<h2 hljs-string">">Recenzie:</h2>
<div id="review-container">
</div>
</div>
</div>
</div>
@endsection
網頁.php:
Route::post('catalog/singleBook/{id}', [CatalogController::class, 'storeReview'])->middleware('auth');

uj5u.com熱心網友回復:
我修好了....路線中缺少'/'。
uj5u.com熱心網友回復:
默認情況下,
一個)。加載表單所在的 HTML 視圖/頁面。
乙)。打開瀏覽器控制臺(Ctrl Shift J)。
C)。單擊“網路”選項卡。然后單擊“網路”選項卡正下方的“Fetch/XHR”選項卡。
d)。在您的頁面上填寫 HTML 表單并嘗試/嘗試在查看“網路”選項卡部分時提交它。您應該會在“網路”選項卡部分看到一個請求,其 URL 類似于您的 Ajax URL 值。
e). Click on the request in the "Network" tab. In your case, you may only see the "book id". I.e: "1" as the request name. But when you hover over the request name, you will be able to see the full path. I.e: "http://base_url_here/catalog/singleBook/1" in the "Network" tab section.
f). Once you've clicked/selected the request, select the "Headers" tab.
g). In the "Headers" tab section, scroll down until you reach the "Request Headers" portion of the list.
h). Look at the headers list in the "Request Headers" portion and check to confirm if the header X-Requested-With: XMLHttpRequest exists.
If you're having a hard time following these steps, try learning how to inspect network activity.
Addendum:
To know why this header is important when making ajax requests and how it relates to Laravel's request()->ajax() method, check out:
Laravel 4 Ajax check to include XMLHttpRequest (from Magnific Popup)
Detect Ajax Request-Php And Frameworks
Edit:
Make sure that the
Request $requestobject you're injecting in thestoreReview(...)method comes fromIlluminate\Http\Request.This isn't mandatory, but you may want to add
"_method" : "POST"to your request body:
$.ajax({
type: "POST",
data: {
// ...
_method: "POST"
},
// ...
});
- If you fail to receive any success with jQuery's
$.ajax(...)method, try using jQuery's$.post(...)method.
$.post("catalog/singleBook/" bookID,
{
reviewText: reviewText,
bookID: bookID,
userID: userID,
_token: $('meta[name="csrf-token"]').attr("content"),
_method: "POST"
}, function (response) {
alert("X");
console.log("X");
$("#review-container").prepend(
"<div id='review'>" response.reviewText "</div>"
);
$("#review-form")[0].reset();
},
"json"
).fail(function () {
console.log(error);
console.log("RREEER");
});
- I strongly believe your jQuery Ajax URL doesn't match the
web.phpfile route definition shared in your question.
Ajax POST request URL.
url: "catalog/singleBook/" bookID,`
web.php Route.
Route::post('missingbook', [MissingBookController::class, 'storeBook'])->middleware('auth');
The route endpoint in your web.php file points to /missingbook yet your jQuery Ajax method URL setting points to catalog/singleBook/{id}. You may want to cross-check/confirm if you forgot and shared the wrong web.php file route definition in your question.
- This isn't related to the issue at hand, but I thought you may find it helpful.
5a). In your HTML markup/view, you refer to the <textarea> tag with id="reviewText exampleFormControlTextarea1"
<textarea class="form-control" name="reviewArea" id="reviewText exampleFormControlTextarea1" rows="3" placeholder="Napí? recenziu.." required></textarea>
Yet you refer to that element in your .js JavaScript file using the "#reviewText" selector:
let reviewText = $("#reviewText").val();
Those two don't match in my humble opinion.
我建議您查看 jQuery 的.serializeArray()以允許您自動構建請求正文,而不必手動進行。
請注意,id屬性值不得包含空格(空格、制表符等)。
5b)。其次,您<textarea>使用請求引數鍵發送元素值,就像reviewText在 jQuery 的 Ajax 請求正文中一樣:
$.ajax({ // ... data: { reviewText: reviewText, // ... } // ... });
然而,在您的控制器的storeReview(...)方法中,您嘗試使用$request->reviewArea
public function storeReview(Request $request) { // ... if($request->ajax()) { // ... $review->reviewText = $request->reviewArea; // ... } }
這兩個也不配。$request->reviewArea肯定會永遠回來null。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/411068.html
標籤:
