Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch() always results in "type: 'opaque'". But I can use Postman on my endpoint successfully

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:

result of fetch() against my REST endpoint

Here's what I get from Postman:

REST call result from Postman

What am I doing wrong?

like image 456
Chris76786777 Avatar asked Oct 23 '25 03:10

Chris76786777


1 Answers

You 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.

like image 166
Mikhail Shilkov Avatar answered Oct 25 '25 19:10

Mikhail Shilkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!