Encountering Parsing error: Anticipated token is missing, expecting "," within Vue framework

I have been attempting to make an axios request, but no matter what I try, I keep encountering the same error message.

The specific error message reads:

Parsing error: Unexpected token, expected ","
. The error specifically points to axios.get (highlighting the period between "axios" and "get" in the error). This error persists even if I use something like console.log in the code.

I am simply working with a standard default vue project, and the only modification I have made is adding the following code into HelloWorld.vue.

<script type="text/javascript>
   import axios from 'axios';
 //  const axios = require('axios');

  export default {
    name: 'HelloWorld',  //  console.log("hi"),
    props: {
      msg: String //response
    },
    methods: {
      axios.get('https://swapi.co/api/people/1')
        .then(function(response){
            console.log(response)
        })
    }
  }

</script>

If someone could kindly explain why this error persists, I would greatly appreciate it. I assume it's something simple, but at this point, it's driving me crazy. Thank you.

Answer №1

If you try to access your axios method, you may encounter issues as it is not defined as a function. Vue methods can be written in two equivalent ways.

export default {
  data() {
    item: []
  },
  methods: {
    getStarWarsCharacters() {
      // axios call goes here
    }
  }
}

Alternatively, you can write it like this:

export default {
  data() {
    item: []
  },
  methods: {
    getStarWarsCharacters: () => {
      // axios call goes here
    }
  }
}

It seems that Vue automatically converts the first way of writing to the second way.

Answer №2

To ensure axios is accessible throughout your project, it needs to be made global in your main file.

    const axios = require('axios');
global.axios = axios;

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

Find the differences between the values in two arrays of objects and eliminate them from the first array

const arrayOne = [ { id: 22, value: 'hello' }, { id: 33, value: 'there' }, { id: 44, value: 'apple' } ]; const arrayTwo = [ { id: 55, value: 'world' }, { id: 66, value: 'banana' }, ...

Running a function on a specific route within the same file in Node.js - here's how to do it

I am looking to execute a function on a specific route that is defined within the same file. module.exports.controller = function(app) { app.get('/folders/create', createDirectory); } var createDirectory = function(path, name, permissions, v ...

Fill the table according to the selected criteria

One issue I am facing is that the table does not get cleared when using the arrow keys to navigate through select options. As a result, the data from the JSON is populated in sequence and does not match the currently selected option. Does anyone have any ...

Customize the behavior of jQuery cycle with an <a> tag

Can the jQuery cycle be accessed through a link in order to override the i variable? I've come across examples that can achieve this, but not in a scenario like this where the cycle() function is located within a variable: $(document).ready(function ...

I'm experiencing difficulties with the "AXIOS PATCH" functionality

I am attempting to modify a database record using the "axios patch" method. This is the code I have tried: editClient(client) { let data = new FormData(); data.append("name", this.client.name); data.append("email", this.client.email); data.append ...

Replicate the function of the back button following the submission of an ajax-submitted form to Preview Form

I am currently working on a multi-part form with the following data flow: Complete the form, then SUBMIT (using ajax post) jQuery Form and CodeIgniter validation messages displayed if necessary Preview the submitted answers from the form Options: Canc ...

The function of AJAX is to send and receive data asynchronously without

Recently, I was experimenting with AJAX. When I use echo "hello" in my PHP file, everything works perfectly. However, if I try something like echo "<script language=Javascript> alert('hi');</script>"; in the PHP file, the alert ...

Tips for securely integrating freelancers into your web project

Looking for a secure way to bring in freelancers to assist with tasks on our website, where the freelancer only has write access to specific pages. I'm aware that this can be done with tools like Windows Team Foundation Server. However, I need the fr ...

Issue with Vuejs2.6: Images failing to load

Despite searching through various questions on this topic, I have not found a solution that works for me. I am attempting to create a basic Image component, but the images are failing to load after I integrated them into a component. <template> & ...

"Setting a maximum height for a div element, along with nested divs and paragraphs

.wrap { width:400px; height:200px; border:1px solid #000; } .content { width:300px ...

What distinguishes the act of altering attributes using React.createContext() in comparison to the value property?

Imagine you have the code snippet below: //App.js //logoutHandler function defined here function App { const testContext = React.createContext({ isLoggedIn: false, onLogout: logoutHandler }) return ( <testContext.Provider> //Some code w ...

The initial element within the div style is malfunctioning

Could someone assist me in understanding why the first-of-type CSS is not working correctly? .item:first-of-type .delete{ display: none ; } .delete { text-decoration: none; color: red; padding-top: 40px;} .add_form_field { white-space: nowrap; } < ...

Error in server file of NextJS due to Typescript issue

My current setup includes the following package versions: next v8.1.1-canary.42 @types/next v8.0.5 server.js import next from 'next'; const app = next({ dev: isDevelopment }); tsconfig.json { "compilerOptions": { "allowJs": true, ...

Is there a way to utilize Javascript to retrieve elements without specifying an id?

I am faced with a challenge of accessing an element without an id but with a shared classname using Javascript. How can I accomplish this task successfully? My attempt to use the document.getElementsbyClassName property was unsuccessful due to the shared ...

What is the reason that textContent assignment does not function properly when used with HTML tags?

I have been attempting to insert additional text into an existing element using the textContent property. However, when I use the += operator for addition assignment, it seems to strip away the formatting of pre-existing tags like b. I expected that the ne ...

Ways to retrieve the current URL in Next.js without relying on window.location.href or React Router

Is there a way to fetch the current URL in Next.js without relying on window.location.href or React Router? const parse = require("url-parse"); parse("hostname", {}); console.log(parse.href); ...

Is there a way to eliminate validation-on-blur errors triggered by onBlur events?

I am currently working on a v-text-field that has the capability to handle simple math expressions like 1+1 and display the correct result (2) when the user either presses enter or moves away from the text field. Here's the code I have implemented so ...

The Google Books API initially displays only 10 results. To ensure that all results are shown, we can implement iteration to increment the startIndex until all results have

function bookSearch() { var search = document.getElementById('search').value document.getElementById('results').innerHTML = "" console.log(search) var startIndex = I have a requirement to continuously make Ajax calls ...

Mastering AJAX with Django

I'm currently struggling to understand how to create an AJAX function, as all the tutorials I've come across seem too complicated for me. This is my HTML code: <a href="{% url pay_provider id=statement.statement_id %}" id="pay_provider">P ...

Can you explain the concepts of 'theme' and 'classes'?

Currently, I am working on a web application using React. I have chosen to incorporate the latest version of Material-UI for designing the user interface. During this process, I came across the demo examples provided by Material-UI (like ) In each demo ...