javascript - DocumentDB 模拟器 REST API

标签 javascript rest emulation azure-cosmosdb

我正在本地针对 DocumentDB 模拟器测试一些代码,但 REST API 调用未通过。我从 Chrome 收到以下错误:

XMLHttpRequest cannot load https://localhost:8081/dbs.
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'null' is therefore not allowed access. The response had HTTP status code 401.

我的代码位于本地文件中,并且我还使用 DocumentDbEmulatorCertificate 创建了本地 IIS 站点。这两个都收到相同的错误。

Fiddler 工作正常,以下请求返回数据库列表:

User-Agent: Fiddler
x-ms-date: Thu, 16 Feb 2017 00:32:08 GMT
Authorization: type%3dmaster%26ver%3d1.0%26sig%3dbpV9cfJJaOpXeGYwTxM8u3LtODh61EbiKw74d%2bnZCdY%3d
x-ms-version: 2016-07-11
Cache-Control: no-cache
Accept: application/json
Content-Type: application/json
Host: localhost:8081

我用来发出请求的代码如下:

<button onclick="db.getList()">Get DBs</button>

var res = document.getElementById("resultText");

        var db = {
            getList: function () {

                let stamp = "Thu, 16 Feb 2017 00:32:08 GMT";
                let token =
                    "type%3dmaster%26ver%3d1.0%26sig%3dbpV9cfJJaOpXeGYwTxM8u3LtODh61EbiKw74d%2bnZCdY%3d";

                res.innerText = '> New request...\r> ' + new Date() + "\r";
                request.send("GET", "https://localhost:8081/dbs", null, stamp, token, function (data) {
                    res.innerText += "> " + data + "\r";
                });
            }
        };

        var request = {
            send: function (method, url, data, stamp, token, callback) {
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (this.readyState == 4 && this.status == 200) {
                        callback(this.responseText);
                    } else
                    if (this.readyState == 4 && this.status != 200) {
                        res.innerText += "> " + this.status + ': ' + this.responseText + "\r";
                    }
                };
                xhttp.open(method, url, true);
                xhttp.setRequestHeader("x-ms-date", stamp);
                xhttp.setRequestHeader("Authorization", token);
                xhttp.setRequestHeader('x-ms-version', '2016-07-11');
                xhttp.setRequestHeader("Cache-Control", "no-cache");
                xhttp.setRequestHeader('Accept', 'application/json');
                //xhttp.setRequestHeader("Content-Type", "application/json");
                //xhttp.withCredentials = true;
                xhttp.onerror = function (e) {
                    res.innerText += '> There was an error!\r';
                };

                xhttp.send(data);
            },
        };

        res.innerText += '> Ready\r';
body {
  padding: 1em;
  font-size: 1em;
  font-family: sans-serif;
}

#resultText {
  border: 1px solid silver;
  padding: 1em;
  font-size: 0.7em;
  font-family: Courier New, Courier, monospace;
}
<!DOCTYPE html>
<html>

<head>
    <title>DocumentDB REST API Test</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
</head>

<body>
    <h3>Document DB</h3>
    Requests
    <p>
        <button onclick="db.getList()">Get DBs</button>
    </p>
    <div id="resultText"></div>
 </body>

知道为什么我会遇到 CORS 问题吗?当然,使用模拟器我应该能够调用本地电话?

最佳答案

这是由您的浏览器而不是本地模拟器强制执行的。你可以

  • 尝试在禁用网络安全的情况下重新启动浏览器。对于 Chrome,所需的标志是 --disable-web-security --user-data-dir。如果这有效,那么你应该...
  • 导航到 chrome://net-internals/#hsts 并从需要 hsts 的域列表中删除 localhost

此外,下载最新版本的 DocumentDB 模拟器。

关于javascript - DocumentDB 模拟器 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42263088/

相关文章:

angular - 尝试使用字符串作为有效负载执行 http.post 请求

java - 在 Java 中嵌入极简 Unix 操作系统

emulation - GameBoy 16 位加载到 8 位内存

assembly - Z80 指令寄存器大小

Javascript 折叠延迟功能

javascript - 使用atan2的 Angular 动画

node.js - 如何使用 Express.js 从 Node.js 连接到第三方 API

javascript - 使用 Angular "number"过滤器键入时,如何在输入字段中使用千位和小数分隔符格式化字符串?

javascript - 如何处理 NodeJS 模块中的 Assets

java - 如何在不使用 spring 的 @Transactional beans 的情况下管理 jax-rs 中的事务