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 pletely different story and I'm getting nowhere.
I've tried both XMLHttpRequest and fetch() and I've tried so many different binations 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 = "=="
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?
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 pletely different story and I'm getting nowhere.
I've tried both XMLHttpRequest and fetch() and I've tried so many different binations 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/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?
Share Improve this question asked May 18, 2017 at 21:19 Chris76786777Chris76786777 7391 gold badge13 silver badges24 bronze badges1 Answer
Reset to default 5You request the URL in no-cors
mode, which is why opaque response is returned. Effectively, that's what you asked for.
Instead, I suggest you configuring CORS for Azure Function as described here and changing mode to cors
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744750562a4591569.html
评论列表(0条)