So I'm trying to do an API call using Ajax on the Admin page, when I add this API call in onclick attribute, it doesn't work it returns error 400 Bad request, BUT when the API call isn't vinculated at onclick, it works.
using onclick:
$('#button').on('click', function(){
var settings = {
// "url": "http://"+url+"/rest/V1/orders",
"url": "<?php echo $block->getBaseUrl()."rest/V1/orders" ;?>",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer "+"<?php echo $token; ?>"
},
"data": JSON.stringify(
{
"entity":{
"entity_id": order_id,
"status":"processing",
"state":"processing"
}
}
),
}
$.ajax(settings).done(function (response) {
console.log(response);
});
});
NOT using onclick (it works):
var settings = {
// "url": "http://"+url+"/rest/V1/orders",
"url": "<?php echo $block->getBaseUrl()."rest/V1/orders" ;?>",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer "+"<?php echo $token; ?>"
},
"data": JSON.stringify(
{
"entity":{
"entity_id": order_id,
"status":"processing",
"state":"processing"
}
}
),
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Any help is appreciated.