6

I have one application in angularJs where I use $location.protocol() + '://' + $location.host() when I'm dealing with API calls.

Example:

return $http({
    method: 'GET',
    url: $location.protocol() + '://' + $location.host() + '/rest/api/category/category',
    headers: {
        'Jwt-token': store.get('jwt')
    }
})

Now I'm building another application in VUEjs and i also wan't to use same "$location" logic to call API, but I don't know how.

My current implementation is hardcoding url

Example:

getCategories() {
    fetch(`http://myapp.test/rest/api/category/category`, {
        method: 'GET'
    })
    .then(response => response.json())
    .then(json => this.categories = json)
}

How to properly "translate/convert" the code ($location.protocol() + '://' + $location.host()) from my Angular app to VueJs? If you need any additional information's please let me know and I will provide! Thank you!

3
  • Why don't you just use relative urls fetch('/rest/api/category/category')? Commented Dec 29, 2017 at 12:33
  • 1
    Use any of these: (1) relative URLs, (2) Protocol relative URLs (ex: //myapp.test/rest/api/category/category) or (3) window.location.protocol + "://" + window.location.host Commented Dec 29, 2017 at 12:35
  • Because this VUE app is running via npm run dev (so localhost:8080), other REST api application is currently runed via MAMP (so via virtualhost myapp.test) Commented Dec 29, 2017 at 12:36

1 Answer 1

13

Use can use the DOM api inside Vue applications:

  • document.location.protocol for protocol
  • document.location.host for the current host
  • or just document.location.origin for protocol+'://'+host
Sign up to request clarification or add additional context in comments.

Comments

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.