سلام ، من از کد زیر برای ارسال درخواست ajax در یک فایل blade.php استفاده کردم:
<body>
<button type="button" onclick="loadDoc()">Request data</button>
<span id="err">error</span>
<head>
<script>
function loadDoc() {
var response;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// document.getElementById("demo").innerHTML = this.responseText;
console.log(response.success);
}
};
xhttp.open("GET", "rout5", true);
xhttp.send();
}
</script>
</head>
</body>
کنترلر:
public function AjaxController(){
return response()->json(['success' => 'hello world']);
}
روت:
Route::get('rout5',[AjaxController::class,'AjaxController']);
موقع اجرا برنامه ، در تب network مقدار 200 رو دریافت می کنم ولی در تب console ، ارور response not definde رو دریافت می کنم ، مشکل از چیه ؟ چجوری باید response رو تعریف کنم که اونو بشناسه ؟ تشکر