I must be really terrible at JavaScript but I've been struggling with this for a few days and I've not made any progress.
To make a long story short, I'm trying to work with UPS's REST API. I can do it with Postman and I can do it with PowerShell without any problems. JavaScript is a completely different story and I'm getting nowhere.
I've tried both XMLHttpRequest and fetch() and I've tried so many different combinations of things I can't begin to list them all.
Below is my JS function in my web app (it's triggered onchange of a field). The JS function makes a call to an Azure Function (the Azure Function works from Postman and from PowerShell.)
function getUpsShipTime() {
var jsonBody = {
"DeliveryDate": "2017-06-06",
"ShippingCode": "GND",
"ShipFrom": {
"Address": {
"StateProvinceCode": "CA",
"CountryCode": "US",
"PostalCode": "90210"
},
},
"ShipTo": {
"Address": {
"StateProvinceCode": "FL",
"CountryCode": "US",
"PostalCode": "32830"
}
}
}
var uri = "https://MyAzureFunction.azurewebsites.net/api/HttpTriggerPowerShell1?code=MyAuthCode=="
var req = new Request(uri, {
method: 'post',
mode: 'no-cors',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(jsonBody)
});
fetch(req)
.then(function (response) {
console.log(response);
return response.blob();
}).then(function (blob) {
console.log(blob);
});
}
When the function runs I get the following:
Here's what I get from Postman:
What am I doing wrong?