2017-09-06 18 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"); 
    } 
}); 

ブラウザ結果:

答えて

1

{"response":"Missing attributes: Basket ID, Title, Merchant Name, Merchant URL, Product URL"}この

$.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

方法が異なります。カールはPOSTではなくGETを使用しています。 –

+0

GETが機能しました。あなたがた両方に感謝します! – Dtrav

関連する問題