将Curl请求转换为jQuery AJAX

时间:2017-09-07 12:24:53

标签: jquery ajax api curl

除了网站之外,我在-d之后无法获取所有内容。卷曲命令:

curl -H “Authorization: Token #{auth_token}” -X GET -d ‘basket_id=#{basket_id}&price=#{price}&title=#{title}&merchant_url=#{merchant_url}&comment=#{comment}&product_url=#{product_url}&merchant_name=#{merchant_name}&color=#{color}&size=#{size}&product_image_url=#{product_image_url}’ http://localhost:3000/api/v1/baskets/add

这是我到目前为止所做的:

$.ajax({
        url: "http://localhost:3000/api/v1/baskets/add",
        type: 'GET',
        processData: false,
        headers: { 'Authorization' : token_string },
         data: "'basket_id=1&price=22800&title=Tory%20Burch&merchant_url=https://www.bloomingdales.com&product_url=https://www.bloomingdales.com/shop/product/tory-burch-minnie-travel-ballet-flats?ID=1830976&CategoryID=16963#fn=ppp%3D%26spp%3D2%26sp%3D1%26rid%3D121%7CBOOST%20SAVED%20SET%26spc%3D492%26rsid%3Dundefined%26pn%3D1%7C6%7C2%7C492&merchant_name=Bloomingdales&color=Black/Gold&size=5&product_image_url=https://images.bloomingdales.com/is/image/BLM/products/2/optimized/9262012_fpx.tif?wid=800&qlt=90,0&layer=comp&op_sharpen=0&resMode=sharp2&op_usm=0.7,1.0,0.5,0&fmt=jpeg'",
        success: function (data) {
          window.response = JSON.stringify(data);
          console.log(response);
          console.log(data);
        },
        error: function(){
         console.log("Cannot get data");
        }
    });

这是我在浏览器中收到的回复:{"response":"Missing attributes: Basket ID, Merchant Name"}

我还有其他curl命令,例如:curl -H “Authorization: Token #{auth_token}” -X GET http://localhost:3000/api/v1/baskets/正常工作,我从服务器得到响应,所以它肯定与授权令牌或链接没有任何关系。任何帮助将不胜感激。

我还应该提一下,ajax请求中的数据字符串减去双引号,在我的终端中完美地运行并且请求通过。

1 个答案:

答案 0 :(得分:1)

问题是因为您已将data包装在双单引号中 - 它应该是一个或另一个:

data: 'basket_id=1&price=22800&title=Tory%20Burch&merchant_url=https://www.bloomingdales.com&product_url=https://www.bloomingdales.com/shop/product/tory-burch-minnie-travel-ballet-flats?ID=1830976&CategoryID=16963#fn=ppp%3D%26spp%3D2%26sp%3D1%26rid%3D121%7CBOOST%20SAVED%20SET%26spc%3D492%26rsid%3Dundefined%26pn%3D1%7C6%7C2%7C492&merchant_name=Bloomingdales&color=Black/Gold&size=5&product_image_url=https://images.bloomingdales.com/is/image/BLM/products/2/optimized/9262012_fpx.tif?wid=800&qlt=90,0&layer=comp&op_sharpen=0&resMode=sharp2&op_usm=0.7,1.0,0.5,0&fmt=jpeg',

虽然您也可以提供一个允许jQuery对您的值进行URL编码的对象,因为您发送的URL中的&符号(&)可能会遇到一些问题。您还需要删除processData: false。试试这个:

data: {
  basket_id: 1,
  price: 22800,
  title: 'Tory%20Burch',
  merchant_url: 'https://www.bloomingdales.com',
  product_url: 'https://www.bloomingdales.com/shop/product/tory-burch-minnie-travel-ballet-flats?ID=1830976&CategoryID=16963#fn=ppp%3D%26spp%3D2%26sp%3D1%26rid%3D121%7CBOOST%20SAVED%20SET%26spc%3D492%26rsid%3Dundefined%26pn%3D1%7C6%7C2%7C492',
  merchant_name: 'Bloomingdales',
  color: 'Black/Gold',
  size: '5',
  product_image_url: 'https://images.bloomingdales.com/is/image/BLM/products/2/optimized/9262012_fpx.tif?wid=800&qlt=90,0&layer=comp&op_sharpen=0&resMode=sharp2&op_usm=0.7,1.0,0.5,0&fmt=jpeg'
}