javascript - 如何使用 BigCommerce utils.api.productAttributes.optionChange

标签 javascript reactjs bigcommerce

如何使用utils.api.productAttributes.optionChange在 React 功能组件上以及当您无权访问 $form.serialize()

我尝试手动创建这样的 formData,但它不起作用。

  const addOption = () => {
    const formData = new FormData();
    formData.append("action", "add");
    formData.append("product_id", "459");
    formData.append("optionValue", "12213");
    formData.append("attribute", "25331");

    console.log(formData);

    utils.api.productAttributes.optionChange(
      459,
      formData,
      (err, response) => {
        const attributesData = response.data || {};

        console.log("err: ", err);
        console.log("res: ", response);

        // I just copied this from the documentation but where do I get these methods?
        this.updateProductAttributes(attributesData); 
        this.updateView(attributesData); 
      }
    );
  };  

我收到了这些errors虽然响应状态代码是200,而我的formData只是空的。

最佳答案

文档中没有对此进行解释,但您实际上不需要序列化表单数据,您只需要属性 ID 和值。试试这个:

const addOption = () => {

  utils.api.productAttributes.optionChange(
    459,
    'attribute%5B25331%5D=12213',
    (err, response) => {
      const attributesData = response.data || {};

      console.log("err: ", err);
      console.log("res: ", response);

      // I just copied this from the documentation but where do I get these methods?
      this.updateProductAttributes(attributesData); 
      this.updateView(attributesData); 
    }
  );
};

关于javascript - 如何使用 BigCommerce utils.api.productAttributes.optionChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72907722/

相关文章:

javascript - 容器内的 jQuery 捕捉元素

javascript - 为什么 Map.has() 对确实存在的 Integer 键返回 false?

javascript - 如何在表单提交后保持模态窗口打开并在同一模态窗口中显示新页面

javascript - React Native - 如何使用 AsyncStorage 保存 map

css - 如何在 Stencil 中添加背景图像

e-commerce - 正在访问 Bigcommerce 的 %%GLOBAL_CustomerId%% 变量

html - 根据上面的文字对齐页面上的按钮

javascript - Google Maps API 下拉列表,缩放至标记

javascript - 调用 setState() 改变集合时,是否可以在 React 中返回一个空对象?

javascript - React.js - babel-loader - 意外的 token