2

I am new to js and would like to convert my JQuery-based js to Vue. I want to send a get request and output back the data. What is the best way of doing this?

Here is the html:

<div>
  <div>
    <input type="text" id="var1" placeholder="Search...">
  </div>
  <div>
    <button id="submit">Submit</button>
  </div>
</div>
<div>
  <p>Results</p>
  <p id="results"></p>
</div>

Below is my js:

$(document).read(function() {
  $('#var1').keypress(function(e) {
    if (e.keyCode == 13)
      ('#submit').click();
  });
});

$(document).ready(function() {
  $("#submit").click(function() {
    var var1 = document.getElementById("var1").value

    // sends get request to URL
    $.getJSON("URL" + var1, function(search, status) {
      console.log(search);
      // cont.
      $("#results").text(search.results);
    });
  });
});

EDIT: Here is what I have so far with axios:

function performGetRequest() {
  var var1 = document.getElementById('var1').value;

  axios.get('URL', {
    params: {
      id: var1
    }
  })
  .then(function (response) {
    console.log(search);

  })
}

I am not sure if the above code is correct or how to factor in keypress and click-- is there a simple way to do that?

5
  • 1
    Have you tried anything yet? Nobody is going to do the work for you. Commented Jul 2, 2018 at 19:31
  • @thedarklord47 yes I tried using various directives but not sure how the get request comes in Commented Jul 2, 2018 at 19:43
  • 1
    Check out github.com/axios/axios Commented Jul 2, 2018 at 19:57
  • 2
    if you are using Vue, you should use v-model instead of document.getElementById to get the input value Commented Jul 2, 2018 at 20:34
  • 1
    You should wrap around the input and button with a form tag and handle the submit event in vue Commented Jul 2, 2018 at 20:35

1 Answer 1

3

Well, I am not sure what you want to do with this ajax call, but hopefully this may help you. Vue is data driven, so I always try to focus on that aspect. So this is an example of how you can access and input and send the data using axios.

<div>
  <div>
    <input v-model='input' type="text" placeholder="Search...">
  </div>
  <div>
    <button @click="searchInput()">Submit</button>
  </div>
</div>
<div>
  <p>Results</p>
  <p >{{ result }}</p>
</div>

you must have those models in your data

// Your data
data() {
    return {
        input: '',
        result: '',
    }
}

and your method will look something like this.

searchInput() {

    axios({

        method: 'GET',
        url: url + this.input,

    }).then(response => {

        this.result = response.data;

    }).catch(error => {

        //handle error
    })
}

So this is a very basic example. you can do the same process in different ways, you could pass the input to the method or loop over the results, but the idea is taking advantage of Vue.js data driven system and think data first.

Hopefully this will help you, remember to escape your input and add necessary validations. Good luck

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the detailed response and your patience
Your welcome. I'm can highly recommend watching laracast.com tutorials on Vue and Vue testing, they are a great way to get a good introduction into Vue. Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.