Search code examples
httpvue.jspostexternal

Making a external POST is not working in my Vue code


I have a problem and I've been trying to fix it many days and I can't solve it. I just want to send a hint to Google Analytics when I click one button (to do it, I have to make a request (POST). This is my code, I have deleted the ID that I have in GA, just to show you the original code:

import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)

handleMP () {
this.$http.post('www.google-analytics.comv=1&t=pageview&tid=UA-XXXXXX- 
X&cid=555&dp=%2Fanalytics')
}

The problem is that I don't know why, when I make the POST, the URL I use it is added to http::/localhost:8080/ and I can't make the POST. Example URL I can see in the console: http://localhost:8080/www.google-analytics.com?v=1&t=pageview&tid=UA-XXXXXXX-X&cid=555&dp=%2Fanalytics

How can I fix this? Thanks in advance


Solution

  • I'm pretty sure you're missing a http:// or https:// prefix in the URL, so Vue thinks it's just a relative URL and appends what you entered to the current address.

    Try adding the https://, so it looks like this:

    handleMP () {
    this.$http.post('https://www.google-analytics.comv=1&t=pageview&tid=UA-XXXXXX- 
    X&cid=555&dp=%2Fanalytics')
    }