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

Get the productVariant metafields using Ajax and graphQL

Get the productVariant metafields using Ajax and graphQL

CoreyNg
Shopify Partner
41 4 1

May I know if it was possible to get the "productVariant metafields" by using Jquery Ajax and graphQL?
I am able to use the graphQL below to get the response object by using Python and Postman.

query {
        productVariant(id"gid://shopify/ProductVariant/11111111111111") {
            metafields(first10) {
                edges {
                    node {
                        namespace
                        key
                        value
                    }
                }
            }
        }
}
However, I cannot apply it by using jquery. I try to put the code below in one of my template liquid files but return nothing.
----------------------------------------------------------
<script>
var settings = {
  "method""POST",
  "timeout"0,
  "headers": {
    "Content-Type""application/json"
  },
  "data"JSON.stringify({
    query: "query {\r\n        productVariant(id: \"gid://shopify/ProductVariant/1111111111111\") {\r\n            metafields(first: 10) {\r\n                edges {\r\n                    node {\r\n                        namespace\r\n                        key\r\n                        value\r\n                    }\r\n                }\r\n            }\r\n        }\r\n}\r\n",
    variables: {}
  })
};
$.ajax(settings).done(function (response) {
  alert(response);
});
</script>
----------------------------------------------------------------------
Beside ajax, I also try to use fetch. Unfortunately, both of their response is EMPTY.
 
Can anyone teach me how to make it work? Thanks a lot.
Replies 0 (0)