1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ShopifyのカートページでjQuery.postを使ってUncaught SyntaxError: Unexpected token ':'エラーが出た時の対処法

Last updated at Posted at 2021-05-23

ShopifyのカートページでjQuery.postを使ってUncaught SyntaxError: Unexpected token ':'エラーが出た時の対処法

やったこと:ブランクテーマからカートページを作成中、カートのアイテム数量をAjaxを使用して値を更新しようとしていた。
参考にしたのが、こちらhttps://shopify.dev/docs/themes/ajax-api/reference/cart#post-cart-change-js
コード: jQuery.post('/cart/change.js', { quantity: 変更したい値, line: ラインNO. });
操作:数量を変更すると、以下のエラーがずっと出て値が変更されない…
エラー:Uncaught SyntaxError: Unexpected token ':'
約:”書き方間違ってるよー”

ってずっと言われ続けた!

対処法

      jQuery.post({
        url:'/cart/change.js',
        dataType: 'json',
        data: { quantity: 変更したい値, line: ラインNO. }
      });

で、OK。

なぜ?

参考:https://community.shopify.com/c/Shopify-APIs-SDKs/AJAX-POST-cart-add-js-Functions-but-returns-Syntax-Error/td-p/135455
回答の文章中にありました!POSTした先(サーバー側)がデータのタイプを適切に認識できておらず、エラーが出てきていた。
なので、文章中のコードを試してみたができず…
そんなこんなで色んな記事を漁っていたら、上記のコードが書いてある記事のURLがどこに行ったか分からなくなってしまいました!
ごめんなさい。

まとめ

ShopifyのCart APIでUncaught SyntaxError: Unexpected token ':'が出た時は、dataType: 'json'を渡して、データのタイプがJSONなのを教えてあげる!

まだまだ、日本語約の少ないShopify&Liquidですが、翻訳ツールを使いこなして日本のShopifyを盛り上げていきましょう!!
どなたかのお役に立てれば、幸いです!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?