Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Re: shopify ajax call api cros problem

shopify ajax call api cros problem

AL33
Shopify Partner
43 0 3

Hi,

 I am using ajax to call shopify api to get pages.json data in liquid page,

It works before changing the domain name, but there are cross domain issues after changing the domain name.

 

I don’t know how to fix the problem.

Waiting for you reply, thanks.

 

Code:

jQuery.ajax({

                 url: "/admin/api/2022-07/pages.json?title="+title,

                 type: "GET",

                 crossDomain: true,

                 dataType: "json",

                 contentType: "application/json;charset=utf-8",

                 headers: { "X-Shopify-Access-Token": shopifyToken },

                 success: function(result) {

                    if (result.pages.length > 0) {

                     

 

                    }

                 }

                 });

 

Error:

Access to XMLHttpRequest at 'https://xxx.myshopify.com/admin/api/2022-07/pages.json?title=Residential%20District' (redirected from 'https://newxxx.hk/admin/api/2022-07/pages.json?title=Residential%20District') from origin 'https://newxxx.hk' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

 

 

Replies 8 (8)

Akibhusen
Shopify Partner
715 121 149

Please try the below code and check:

 

Shopify.request({
  url: "/admin/api/2022-07/pages.json",
  type: "GET",
  dataType: "json",
  data: { title: title },
  success: function(result) {
    if (result.pages.length > 0) {
      // Handle the success response here
    }
  },
  error: function(xhr, status, error) {
    // Handle any errors that occur during the request
  }
});

 

 

In this code, the Shopify.request function is used to make the API request to the /admin/api/2022-07/pages.json endpoint. The data option is used to pass the query parameter title. The response will be in JSON format, which can be handled in the success callback function.

 

Please note that you need to make sure the code is running within a Shopify theme or a Shopify app to have access to the Shopify Ajax API.

AL33
Shopify Partner
43 0 3

Uncaught TypeError: Shopify.request is not a function

I write the code in the liquid page.

Akibhusen
Shopify Partner
715 121 149

Try the below code and let me know if it works or not.

 

 

<script>
  var url = "/admin/api/2022-07/pages.json";
  var params = { title: {{ title | json }} };
  var headers = { "X-Shopify-Access-Token": {{ shopifyToken | json }} };

  fetch(url + "?" + new URLSearchParams(params), { headers: headers })
    .then(function(response) {
      if (response.ok) {
        return response.json();
      } else {
        throw new Error("Request failed with status " + response.status);
      }
    })
    .then(function(result) {
      if (result.pages.length > 0) {
        // Handle the success response here
      }
    })
    .catch(function(error) {
      // Handle any errors that occur during the request
    });
</script>

 

 

make sure to replace {{ title }} and {{ shopifyToken }} with the appropriate values.

AL33
Shopify Partner
43 0 3

 

CROS problem: 

AL33_0-1684921293751.png

AL33_1-1684921466311.png

 

My shopify website has been set new domain, and redirects....

 

AL33
Shopify Partner
43 0 3

Not working.

Akibhusen
Shopify Partner
715 121 149

Is it possible to access your theme code? I'll check and try some alternative ways to solve the issue.

AL33
Shopify Partner
43 0 3

I don't know how to let you check my theme code.

 

It works before changing the domain name, but there are cross domain issues after changing the domain name and set some redirects.

Is there something wrong with my domains?

AL33_1-1684999873463.png

 

 

 

AL33_0-1684999640830.png

 

Akibhusen
Shopify Partner
715 121 149

You have to update the title with your page and the Shopify token with the generated key in the App. Check attached screenshot to update token key:

 

Akibhusen_0-1685001594900.png