【发布时间】:2018-02-15 01:27:36
【问题描述】:
我有一个需要使用的 API,但我不确定如何通过 AJAX 输入我的 curl 命令。任何帮助将不胜感激。
我的 curl 命令:curl -H “Authorization: Token #{auth_token}” -X GET http://localhost:3000/api/v1/baskets/
到目前为止的 AJAX:
$.ajax({
url: "http://localhost:3000/api/v1/baskets/",
type: 'POST',
dataType: 'json',
contentType: 'application/json',
processData: false,
success: function (data) {
alert(JSON.stringify(data));
},
error: function(){
alert("Cannot get data");
}
});
更新:问题 1 已解决。除了最后一次 API 调用之外,我能够执行类似的方法来得出我需要的结果。除了网站之外,我无法在 -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:
$.ajax({
url: "http://localhost:3000/api/v1/baskets/add",
type: 'GET',
processData: false,
headers: { 'Authorization' : giftibly_token_string },
data: {"basket_id": "2", "title" : "Hello There", "merchant_name" : "Target", "merchant_URL" : "http://test.com", "product_url" : "http://test.com/product" },
success: function (data) {
window.response = JSON.stringify(data);
console.log(response);
},
error: function(){
console.log("Cannot get data");
}
});
浏览器结果:{"response":"Missing attributes: Basket ID, Title, Merchant Name, Merchant URL, Product URL"}
【问题讨论】: