Issue with accessing Vue.js parameter in route

Trying to work with both VueJS and Laravel, I am currently facing an issue where I cannot retrieve a parameter value. Can anyone provide guidance on how to solve this problem?

This is my VueJS code:

getTestData:function () {
    let config = {
        params: {
            id: 1
        }
    };
    axios.post('{{ route('get_user_data') }}', config)
        .then(function (response) {
            console.log(response);
            // app.posts=response.data;
        })
        .catch(error => {

        })
    },

Here is my Controller code:

public function testData(Request $request)
{
    // Need help in retrieving this value
}

My route definition:

Route::post('get-user-data','TestController@testData')->name('get_user_data');

Answer №1

Instead of utilizing a post request to retrieve data from the database, consider using a get request which is more appropriate.

Make sure to include the parameter in the route definition:

Route::get('fetch-user-data/{id}','UserController@getUserData')->name('fetch_user_data');

//For a post request

Route::post('fetch-user-data/{id}','UserController@getUserData')->name('fetch_user_data');

In the controller method, extract the value from the request like this:

public function getUserData(Request $request)
{
    $id = $request->route('id');
}

Answer №2

params refer to the URL parameters that will be included in the request

To access query string input in your controller, you can do so like this:

public function fetchData(Request $request)
{
    $name = $request->query('name');
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React/Javascript - Executing Function returns prematurely

I have been working on a function that takes an object and iterates through it to create a search query. However, the issue I'm facing is that the function returns before I finish looping through the object: export default function buildQuery(query) ...

Managing errors in jQuery.ajax() when it encounters a problem

Having a php file with a form that is submitted through jQuery.ajax(), I encountered an issue where the error message was not displayed if the email failed to send. $(document).ready(function () { $('input[type="submit"]').click(function () ...

Avoid reactivating focus when dismissing a pop-up menu triggered by hovering over it

I am currently utilizing @material-ui in conjunction with React. I have encountered the following issue: In my setup, there is an input component accompanied by a popup menu that triggers on mouseover. Whenever the menu pops up, the focus shifts away from ...

Detecting attribute changes in AngularJS directivesGuide to detecting changes in attributes within Angular

Combining jquery ui and angularjs in my project has been a great experience. I have created a custom directive as shown below. app.directive('datepicker', function() { return { link: function (scope, element, attrs) { elem ...

Preventing Past Dates from Being Selected in JQuery Date Picker

Need help with a date picker that only allows selection of the 1st and 15th dates of each month. How can I prevent users from selecting previous dates? <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" ty ...

Having difficulty retrieving information from Redux store

In my project, I utilize the Redux store to manage data. Through Redux-DevTools, I can observe that initially the data is null but upon refreshing the page, the data successfully populates the store. However, when attempting to retrieve this data within on ...

Encountering the error message "myFunction variable is not declared" when using Google Closure Compiler

When attempting to compile two JavaScript files that both use a function declared in only one of the files, an "undeclared" error is returned. To solve this issue, I added the function declaration to my externs file like this: var myFunction = function() ...

Issue concerning the relative path of RequireJS optimizer

Currently, I am attempting to implement the require optimizer's browser example. My folder structure is set up as follows, with the "r.js" and "build.html" files located at the same level as the "js" folder. js lib | a.js | b.js | ...

Create a line connecting two divs using jQuery's DOM manipulation capabilities

Looking to connect two divs with a straight line, I stumbled upon jQuery DOM line, which appears to offer a more streamlined solution compared to jsPlump. I attempted to incorporate it into my code, but unfortunately, it's not working as expected. Be ...

The presence of double quotes in stringified JSON is undesired

I am currently working on a weather forecasting website where the API returns pure JSON data. However, when I convert this data to a string and add it to an element in order to display it on the webpage, I encounter a problem with double quotes appearing. ...

Retrieve the outcome of an AJAX request in JavaScript within a separate function

My uploadFunction allows me to upload files to my server using a Rest web service called with JQuery ajax. I also use this method for another function and need to determine the result of the web service call in order to add a row with the name of the uploa ...

Asynchronous operations and recursive functions in the world of Node.js

When working with express and mongoose, I frequently find myself needing to perform batch operations on collections. However, the typical approach involves callbacks in nodejs concurrency coding, which can be cumbersome. // given a collection C var i = 0 ...

Dealing with 'ECONNREFUSED' error in React using the Fetch API

In my React code, I am interacting with a third party API. The issue arises when the Avaya One-X client is not running on the target PC, resulting in an "Error connection refused" message being logged continuously in the console due to the code running eve ...

When using Vuepress behind a Remote App Server, pages containing iframes may return a 404 error

Creating a static website using Vuepress was a breeze, especially since I included a dedicated section for embedded Tableau dashboards. The website functioned perfectly when accessed online without any issues, displaying the Tableau dashboards flawlessly. ...

What's the best way to animate the navigation on top of an image for movement?

I am currently in the process of creating my website as a graphic designer. My unique touch is having the navigation positioned on top of an image (which is animated via flash). This setup is featured prominently on my homepage, which is designed with mini ...

Limiting the number of rows in pagination using a select option

Having a pagination feature on my page, I now aim to add a new functionality. When the user selects the number of rows from a drop-down menu, the webpage should display matching data. Despite trying to implement this with ajax, the limit variable is not be ...

Vue paginated select with dynamic data loading

My API has a endpoint that provides a list of countries. The endpoint accepts the following query parameters: searchQuery // optional search string startFrom // index to start from count // number of options to return For example, a request with searchQu ...

How can I pass the current value of an HTML.DropDownListFor to an ActionLink?

Is it feasible to transfer the current value of @Html.DropDownListFor to an action link? I am trying to send the template value to the Sample controller using the Create action. The code below is not functioning because @Model.SurveyTemplate does not retur ...

Sending CSV files to users using MEANJS

Currently, I am employing the MEANJS framework to create a Node.js application. Essentially, I have JSON data stored in MongoDB and I am utilizing the json-csv NPM module to convert it into a CSV format. I managed to successfully download the CSV file loc ...

ajax call not yielding any results

This is a basic AJAX test where I am trying to pass a variable 't' from my index.php to process.php and then alert the result (15) when clicking a button. However, I am facing an issue as nothing is being alerted. Below is my index.php code: &l ...