2017-09-06 92 views
0

我有一个我需要使用的API,但我不确定如何通过AJAX输入我的curl命令。任何帮助,将不胜感激。将curl命令转换为AJAX

我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"); 
    } 
}); 

UPDATE:问题1是固定的。除了最后一次API调用之外,我能够做出类似的方法来获得我需要的结果。除了网站之外,我无法在-d之后获取所有内容。 curl命令: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"}

回答

1

这应该工作

$.ajax({ 
    url: "http://localhost:3000/api/v1/baskets/", 
    type: 'GET', 
    processData: false, 
    headers: { 'Authorization' : 'Token #{auth_token}' }, 
    success: function (data) { 
     alert(JSON.stringify(data)); 
    }, 
    error: function(){ 
     alert("Cannot get data"); 
    } 
}); 
+0

有我的回答和你之间没有什么区别。 –

+0

方法不同。 curl使用GET而不是POST。 –

+0

GET工作。谢谢你们俩! – Dtrav