-2

我在使用 json.stringify 时遇到问题,它在 json 之后返回一个带有冒号的 json

axios.post('http://54.196.61.131/api/v0/user/250/product/add/scan',
        JSON.stringify({
      
          name: currentProduct.product.product_name_fr,
          brand: currentProduct.product.brands,
          image: currentProduct.product.image_front_thumb_url,
          product_quantity: currentProduct.product.quantity,
          ingredients: currentProduct.product.ingredients_text,
          quantity: parseInt(quantite, 10),
          nutriscore_grade: currentProduct.product.nutriscore_grade,
          expiration_date: expDate,
          barcode: currentProduct.code,
        }))

它最后返回一个 : ,所以我的 API 出现 500 错误:

{"name":"Farine de blé suprême","brand":"Francine","image":"https://static.openfoodfacts.org/images/products/306/811/070/3232/front_fr.37.100.jpg","product_quantity":"1 kg","ingredients":"Farine de blé type 45.","quantity":6,"nutriscore_grade":"a","barcode":"3068110703232","expiration_date":"2020-07-24T10:41:04.000Z"}: 

谢谢

4

1 回答 1

0

尝试序列化您的数据。

serialize = function (obj) {
    var str = [];
    for (var p in obj)
        if (obj.hasOwnProperty(p)) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
    return str.join("&");
}
axios.post('http://54.196.61.131/api/v0/user/250/product/add/scan', serialize({
    // Your JSON data
})
于 2020-07-15T12:54:25.037 回答