【问题标题】:Unable to get the API result using javascript code无法使用 javascript 代码获取 API 结果
【发布时间】:2015-01-16 09:23:23
【问题描述】:

我正在处理 API managementexposing API using Javascript code 如果我去Developer portalcheck 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>

我需要在哪里调试?

【问题讨论】:

  • 你遇到什么样的错误?
  • 可能是 XSS 问题?
  • 没有错误 :( 究竟什么是跨站脚本问题?
  • 使用 Fiddler (telerik.com/fiddler)、浏览器中的开发者工具(Chrome 和 Internet Explorer 中的 F12)或 Runscope (runscope.com) 等托管服务充当 API 的调试代理。我会先从您的浏览器内工具开始,然后使用“网络”选项卡查看 API HTTP 调用返回的响应(400 / 500 响应范围内的任何响应都不好)。
  • @SimonW W 感谢 F12 获得 200 状态代码后,请求正文中没有数据:(

标签: javascript azure-api-management


【解决方案1】:

确保您已使用正确的策略在 API 上启用 CORS 支持。 Azure documentation site 有一个如何执行此操作的示例(下面的 sn-p):

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

【讨论】:

  • 我需要在哪里配置:O 我已经安装了这个 Install-Package Microsoft.AspNet.WebApi.Cors
  • 此策略是通过 Azure API 管理的发布者门户添加的。
猜你喜欢
  • 2023-04-01
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多