Jump to content

how to use ajax in laravel?


ali_254
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

Posted (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 by ali_254
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.