Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

json - Creating cURL POST request for API in jQuery?

I'm having some trouble getting a POST request to fire off for an app which we are currently using on a Shopify store.

Essentially, we need to have the following request post to the LoyaltyLion API in order for customers to obtain a reward.

Here's what the cURL request looks like:

  curl -X POST 
 --user 'abc123:abc123' 
 --header 'Content-Type: application/json' 
 --url 'https://api.loyaltylion.com/v2/activities' 
 --data '{
   "name": "qr_code",
   "customer_id": "12345",
   "customer_email": "[email protected]",
   "merchant_id": "12345"
 }'

I've been reading some docs, and this is what I've come up with. I haven't had any luck getting this to fire off on the front end though.

   $.ajax({
url: "https://api.loyaltylion.com/v2/activities'",
beforeSend: function(xhr) { 
  xhr.setRequestHeader("Authorization", "Basic " + btoa("abc123:abc123")); 
},
type: 'POST',
dataType: 'json',
contentType: 'application/json',
processData: false,
data: '{"name": "qr_code", 
        "customer_id": "12345",
        "customer_email": "[email protected]",
        "merchant_id": "12345"
        }',
success: function (data) {
  alert(JSON.stringify(data));
},
error: function(){
  alert("Cannot get data");
}
});

Any help greatly appreciated :)

question from:https://stackoverflow.com/questions/65852432/creating-curl-post-request-for-api-in-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...