Creating Price rules ajax status 303 or 400

Developer07
New Member
4 0 0

Hello!

I'm trying to create a price rule using the Admin API from the documentation, but I have a problem: I get a strange 303 status on post request. If I add contentType: "application / json" I get 400 bad request. Please tell me what could be the problem? Thank you.

Code (with test static post data):

$.ajax({
url: '/admin/api/2021-01/price_rules.json',
contentType: "application/json",
type: 'POST',
data: {
"price_rule": {
"title": "TESTDISCOUNT",
"target_type": "line_item",
"target_selection": "all",
"allocation_method": "across",
"value_type": "fixed_amount",
"value": "-10.0",
"customer_selection": "all",
"starts_at": "2020-01-19T17:59:10Z"
}
},
success: function (response) {
setTimeout(function () {
console.log(response);
},
error: function(xhr) {
console.log('xhr', xhr);
}
})

 

Replies 2 (2)

gina-gregory
Shopify Expert
742 51 211

You can't use the Admin API on the front-end with an Ajax request. You need an app that can properly authenticate on the back-end.

Developer07
New Member
4 0 0

Thank you for answer. Are you sure? GET request works very well.