0

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"}

1 Answer 1

1

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");
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

There is no difference between my answer and your.
Method is different. The curl is using GET not POST.
In GET you can only pass data as querystring. Change your data field as data :‘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}’

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.