0

让我向你解释一下这个问题,我正在使用一个 3rd 方插件,它运行一个 rest 接口,用于在 postgrest 数据库上执行数据库命令。为了限制返回的结果所以我不调用所有1000行是设置请求头。

这是关于此的插件信息:

Limiting

PostgREST uses HTTP range headers for limiting and describing the size of results. Every response contains the current range and total results:

Range-Unit: items
Content-Range → 0-14/15
This means items zero through fourteen are returned out of a total of fifteen -- i.e. all of them. This information is available in every response and can help you render pagination controls on the client. This is a RFC7233-compliant solution that keeps the response JSON cleaner.

To request limits and offsets translate into ranges and then set the request headers

Range-Unit: items
Range: 0-4
You can also use open-ended ranges for an offset with no limit: Range: 10-.

我正在编写自己的 java 脚本库来处理我的项目的其余调用。因此,我试图测试将结果限制为只返回一行。但是我收到以下控制台错误:

Uncaught InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.

这是我的代码: 注意:我将添加另一个 var,因此当您进行调用以获取结果时,您可以设置可编程限制,以使其立即工作。

(function(window){
    function defineCynergi(){
            var Cynergi = {};
            Cynergi.get = function(url){
                var request = makeHttpObject();
                request.open("GET", url, false);
                request.setRequestHeader('Range-Unit', 'items');
                request.setRequestHeader('Range', '0-1');
                request.send(null);
                return JSON.parse(request.responseText);                
            }
            Cynergi.delete = function(url){
                var request = new XMLHttpRequest();
                request.open("DELETE", url, false);
                request.setRequestHeader('Accept', 'application/:3000+json; version=1');
                request.send();
                deleteStatus = request.statusText;
                return deleteStatus;
            }

            Cynergi.insert = function(url, data){
                var request = new XMLHttpRequest();
                request.open("POST", url, false);
                request.setRequestHeader('Accept', 'application/:3000+json; version=1');
                request.send(JSON.stringify(data));
                sentStatus = request.statusText;
                return sentStatus;
            }

            Cynergi.update = function(url, data){
                var request = new XMLHttpRequest();
                request.open("PATCH", url, false);
                request.setRequestHeader('Accept', 'application/:3000+json; version=1');
                request.send(JSON.stringify(data));
                updateStatus = request.statusText;
                return updateStatus;
            }

            return Cynergi;
        }

    if(typeof(Cynergi) === 'undefined'){
        window.Cynergi = defineCynergi();
    }

})(window);

function makeHttpObject() {
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Could not create HTTP request object.");
}

是的,我知道使用 jquery 会更容易,但是对于我正在做的事情 jquery 不起作用,因为它发送的预检查通过以确保帖子可以安全通过与我的插件不兼容. 这段代码是非常早期的 alpha 阶段,但我在大约 15 小时内开发了一个纯 js 社交媒体网站。我只是卡在这些小事情上,因为我缺乏 js 知识。我也知道将它们设置为异步会更好,但我无法在我的生活中弄清楚这一点,尽管我读过的帖子数不胜数,但我似乎无法完全理解它。

4

0 回答 0