Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I pass string variables to controller in laravel via ajax, It doesn't show me proper data

This is my Ajax Query

  function updateTask() {
    var task_id = document.getElementById('edit').getAttribute('edit_task_id');
    var newTask = document.getElementById('edit_task').value;
    var csrf = document.querySelector('meta[name="csrf-token"]').content;
    var close = document.getElementById('close');
    var editob = new XMLHttpRequest();
    editob.open('POST', '/{task_id}/{newTask}', true);
    editob.setRequestHeader('X-CSRF-Token', csrf);
    editob.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    editob.send("task_id=" + task_id, "newTask=" + newTask);
    
    console.log(newTask);
    console.log(task_id);
  }

This is y Controller

    public function editTask($id, $newTask){
        //$x = $id->get('task_id');
         print_r($id);
         print_r($newTask);
         
    }

And this is the response I get, butI actually want the proper string value that I passed through ajax
This is what i get as response

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

actually, your script shows proper data what you asking for

editob.open('POST', '/{task_id}/{newTask}', true);

here your URL looks something like this. because you sending a string instead of a value

http://your-domain.com/{task_id}/{newTask}

what you need to do is

editob.open('POST',  `/${task_id}/${newTask}`, true);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading