5

Cross site ajax request with Vue.js 1.0 and Vue Resource. I get the following error: XMLHttpRequest cannot load http://dev.markitondemand.com/MODApis/Api/v2/Lookup/jsonp?input=NFLX&callback=handleResponse. No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have a basic understanding of the problem but not sure how to add a callback function with the request or if that is the best solution for this example. I put in the full request URL here just to make it easier to follow.

new Vue({
    el: '#stockList',

    data: function() {
        return {
            query: '',
            stocks: []
        };
      },

    ready: function() {
      this.getStocks();

      },

    methods: {
        getStocks: function() {
            this.$http.get('http://dev.markitondemand.com/MODApis/Api/v2/Lookup/jsonp?input=NFLX&callback=handleResponse',
                function(data) {
                  this.stocks = data;
                }
            );
        }
    }
})
3
  • the error message clearly specifies that the server isn't allowing the origin.For cross site requests the server must return/specify the origin(s) that is allowed to request for resources.If you have control over the server please specify this header value in response or * for all. Commented Nov 9, 2015 at 6:17
  • Maybe I don't understand the problem then. I am running a local development server to learn JavaScript and Vue so I have control of the origin server I guess. I do not have control of the markitondemand.com server although if you put that url in the browser it does return a response. I thought by using JASONP it avoided the cross site protocol? Can you explain what I am missing? Thanks Commented Nov 9, 2015 at 15:37
  • Well that seems the way your sending the request is cross site, and the server isn't configured to do so. Commented Nov 9, 2015 at 16:16

3 Answers 3

2

I have almost zero understanding of networking, but I was able to get several remote apis to work using:

this.$http.jsonp

instead of

this.$http.get
Sign up to request clarification or add additional context in comments.

Comments

0

"No Access-Control-Allow-Origin" header usually is a problem with the server. It means that the server is configured to only allow a person access to the API if the request comes from the same domain as the server. You either need to run the script from the website that you are requesting data from, or you need to change the server config to allow access to all domains.

If you don't have access to the server, and you don't want to run the script in the browser, then I think what you could do is use a headless browser like PhantomJS to navigate to the page, insert a script element into the dom that contains you script, execute the function and then return the data from the API. I could write the code out for you, but to be honest, it's a bit complex. You would have to know how to use node.js, and phantom.js. I've personally only used phantom.js for the Node 'html-pdf' package, but I'm sure with a little bit of reading you could figure out how to do it.

Comments

-1

Set your local environment to http instead of https if you have no control over dev.markitondemand.com.

1 Comment

My understanding of AJAX is novice at best so I assumed that markitondemand would accept cross site since they are providing a free API. Perhaps that is not the case, or I am not formatting the request properly. I ended up using PHP for the request to them --- $returned = json_decode(file_get_contents($url), true); --- and then have vue call a local api that I created from their data. Thanks for replying though.

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.