无法使用 javascript 代码获得 API 结果
Unable to get the API result using javascript code
我正在研究 API management
和 exposing API using Javascript code
如果我去 Developer portal
和 check my url
它给我 correct result
如下
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>
哪里需要调试?
确保您已使用正确的策略在 API 上启用 CORS 支持。 Azure documentation site 有一个如何执行此操作的示例(下面的片段):
<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>
我正在研究 API management
和 exposing API using Javascript code
如果我去 Developer portal
和 check my url
它给我 correct result
如下
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>
哪里需要调试?
确保您已使用正确的策略在 API 上启用 CORS 支持。 Azure documentation site 有一个如何执行此操作的示例(下面的片段):
<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>