Dtrav
Dtrav

Reputation: 417

Converting curl command to AJAX

I have an API that I need to use but I'm not sure how to input my curl command through AJAX. Any help would be appreciated.

My curl command: curl -H “Authorization: Token #{auth_token}” -X GET http://localhost:3000/api/v1/baskets/

AJAX so far:

$.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: Problem 1 is fixed. I was able to do similar methods to come up with the results I needed except for this last API call. I'm having trouble getting thing everything after -d except for the website. Curl command: 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 so far:

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

Browser result: {"response":"Missing attributes: Basket ID, Title, Merchant Name, Merchant URL, Product URL"}

Upvotes: 0

Views: 1663

Answers (1)

Vijay Rathore
Vijay Rathore

Reputation: 603

This should work

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

Upvotes: 1

Related Questions