ali_254 Posted July 15, 2021 Share Posted July 15, 2021 hi . i use from this code for send request Ajax in laravel: function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { // document.getElementById("demo").innerHTML = this.responseText; console.log(this); } }; xhttp.open("GET", "/get/subcategory/", true); xhttp.send(); } </script> route: Route::get('/get/subcategory/{category_id}',[PostController::class,'GetSubCategory']); controller: public function GetSubCategory($category_id){ $sub="test"; return response()->json($sub); } for receive request ajax in file blade.php , What command should I use? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/313391-how-to-use-ajax-in-laravel/ Share on other sites More sharing options...
requinix Posted July 15, 2021 Share Posted July 15, 2021 What "command" to do what? 1 Quote Link to comment https://forums.phpfreaks.com/topic/313391-how-to-use-ajax-in-laravel/#findComment-1588356 Share on other sites More sharing options...
ali_254 Posted July 15, 2021 Author Share Posted July 15, 2021 i send Answer the request ajax to file blade.php : public function GetSubCategory($category_id){ $sub="test"; return response()->json($sub); } Now I want to display the answer of the request. How can I display it? (in file blade.php) i want display variable "$sub" in file blade.php Quote Link to comment https://forums.phpfreaks.com/topic/313391-how-to-use-ajax-in-laravel/#findComment-1588357 Share on other sites More sharing options...
ali_254 Posted July 15, 2021 Author Share Posted July 15, 2021 (edited) I found the solution. for display the answer of the request in file blade.php , we can use this cod: controller public function GetSubCategory(){ return response()->json(['success' => 'ok']); } blade.php: <span id="test"></span> Document.getElementById('test').innerText=response.success Edited July 15, 2021 by ali_254 Quote Link to comment https://forums.phpfreaks.com/topic/313391-how-to-use-ajax-in-laravel/#findComment-1588358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.