Neo
Neo

Reputation: 16239

Unable to get the API result using javascript code

I'm working on API management and exposing API using Javascript code If I go to Developer portal and check my url it gives me correct result like below

Response content
[{
"ContactId":1,
"Name":"Debra Garcia",
"Address":"1234 Main St",
},

{"ContactId":2,
"Name":"Thorsten Weinrich",
"Address":"5678 1st Ave W",
}]

but using javascript code I'm not getting anything Nor status error code :(
Js Code
<script type="text/javascript">
    $(function() {
        var params = {

            'subscription-key': 'mykey',
        };

        $.ajax({
            url: 'https://mydemo.azure-api.net/marketing/contacts?' + $.param(params),
            type: 'GET',
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function() {
            alert("error");
        });
    });
</script>

Where I need to debug? enter image description here

Upvotes: 1

Views: 197

Answers (1)

Simon W
Simon W

Reputation: 5496

Make sure that you have enabled CORS support on your API using the correct policy. The Azure documentation site has an example of how to do this (snippet below):

<cors>
    <allowed-origins>
        <origin>*</origin> <!-- allow any -->
        <!-- OR a list of one or more specific URIs (case-sensitive) -->
        <origin>http://contoso.com:81</origin> <!-- URI must include scheme, host, and port. If port is omitted, 80 is assumed for http and 443 is assumed for https. -->
    </allowed-origins>
</cors>

Upvotes: 2

Related Questions