How to make an AJAX call to a middleware API

Highlighted
New Member
3 0 0

I'm trying to make an AJAX call from the shopify frontend, I'm aware that using shopify storefront API with AJAX is a terrible idea since I would expose the credentials, so I created a Node API middleware to handle the request. 

Now I'm trying to call my personal API from the store frontend but I'm getting the  "has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." error.

Is it possible to make any http request from the frontend?

Thank you

this is my call 

 

$(document).ready(function() {
    $.ajax({
        type: 'POST',
        url: 'MYAPIURL,
        dataType: "json",
        success: function(data) {
            console.log(data);
        }
    });
});

 

0 Likes
Highlighted
New Member
3 0 0

Well truns out the AJAX call was correct, I forgot to add this code to my node API

 

app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); 
    res.setHeader('Access-Control-Allow-Credentials', true); 
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers, X-Requested-With');


  	next();
})
0 Likes