Automatic Ajax request

Published 3 weeks ago by david001

Is there any tricks to send ajax data to controller without click event in view. I mean when user visit to the home page(url:'/home') i want to send some data to another url(ie: '/getValue')without clicking submit button.

My js in home.blade.php is

<script type="text/javascript">
       
        var username = "john";
       
        $.ajax({
            type:"GET",
            cache:false,
            url:"/getValue",  

            data:{username:username},
        
 
          success: function(data) {
  
          }
        });

</script>

my controller is

public function receiveData(Request $request)
{
    return $request->username;
       // i need to get here "john" 
}

Route


Route::get('getValue','[email protected]');

How can i get username 'John'to controller

topvillas

Use a post request.

david001

i use trick like this but i won't work

 var url = window.location.href; //to get current url

       if(url = "http://localhost:8000/home"){  //if ```var url``` is equal to home url,fire ajax 
request

        var username = "john";
       
        $.ajax({
            type:"GET",
            cache:false,
            url:"/getValue",  

            data:{username:username},
        
 
          success: function(data) {
  
          }
        });
}
Snapey
Snapey
3 weeks ago (705,395 XP)

why are you trying even to pass a value from your server back to your server?

explain what you are trying to do (the bigger picture) because what you are doing is really odd

david001

var username = "changeable coordinate"; i just want to pass coordinate ie: var latitude = position.coords.latitude; to controller when user visit to my site

suppose i can get latitude and longitude in browser:console.log(position). i want to send it to controller so that based on user lat and lng i can recommend nearest things to user

Snapey
Snapey
3 weeks ago (705,395 XP)

so why give an example passing "john"

what you are saying is that this is a duplicate of your other question

Cronix
Cronix
3 weeks ago (175,190 XP)
$(function() { 
    // whatever code is here will be executed when the page loads
});

Put your ajax in there.

Also, since you're sending data, use post instead of get. Both for the route and the ajax call. Get should only GET data, not send it.

robrogers3

FYI, the $(function() {}) is jquery for onload (when the dom is ready to start handling js events - generally)

you don't need jquery to do it. you can register an event for the onload event like so

document.addEventListener("DOMContentLoaded", function() {
  // code... for ajax here you can also check the href for conditionally doing it.
});
seyidaniels

You should use a post request instead and check out javascript functions when a page has finished loading.... So u send the ajax request within the function

mcstepp

The takeaway here is that you need an event to trigger this request. If you don't want to use a click event, use DOMContentLoaded or another DOM event to trigger your ajax request.

Snapey
Snapey
3 weeks ago (705,395 XP)

@mcstepp why? Script tags are executed on loading. Granted, you might actually need to wait for the dom to be ready to interact with it reliably. Even adding the listener is executing javascript.

Just needs a self invoking function that calls the ajax post.

<script>
(function () {

        var username = "john";
       
        $.ajax({
            type:"POST",
            cache:false,
            url:"/coordinates",  

            data:{username:username},
        
 
            success: function(data) {
                console.log('posted');
          }
        });
});

</script>

david001

i tried all steps mention above but i am not getting value in controller

Snapey
Snapey
3 weeks ago (705,395 XP)

i tried all steps mention above but i am not getting value in controller

how do you know?

RamjithAp

@david001 You are missing single quotes around 'username' parameter. Try this

<script>
(function () {
        var username = "john";
        $.ajax({
            type:"GET",
            cache:false,
            url:"{{ url('/') }}/getValue",  
            data:{'username':username},
            success: function(data) {
                console.log('posted');
          }
        });
});
</script>

Please sign in or create an account to participate in this conversation.