简体   繁体   中英

How to pass a variable from ajax url parameter to laravel route?

I want to pass the sender id to laravel route like this, but seems my code is not working. Please specify the correct way to do it.

url: '/agent_close/{sender_id}',


function  closeChat(){
  sender = document.getElementById("sender_id").value;
  $.ajax({
           type: "POST",
           url: '/agent_close/{sender_id}',
           data: "sender_id=" + sender, 
           success: function (data) {
                console.log('Message Closed');
                console.log(data);
            },
            error: function (data) {
                console.log('An error occurred');
                console.log(data);
            },
         });
}

Route is:

Route::post('/agent_close/{sender_id}', 'AgentController@closeAgentThread');

Function:

   public function closeAgentThread($sender_id){
        $bot = new BotManController();
        return $bot->TakeThreadControl($sender_id);
    }
url: '/agent_close/' + sender,
  sender = document.getElementById("sender_id").value;
  $.ajax({
           type: "POST",  //you can use GET instead of POST
           url: '/agent_close/'+sender,   //this line used Route_URL instead Route_name
           data: "",   //can be null
           success: function (data) {
                console.log('Message Closed');
                console.log(data);
            },
            error: function (data) {
                console.log('An error occurred');
                console.log(data);
            },
         });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM