使用同步XMLHTTPRequest设置Javascript请求标头

时间:2015-04-12 07:05:22

标签: javascript jquery postgrest

让我向您解释一下这个问题,我使用第三方插件运行休息接口,通过postgrest数据库执行数据库命令。为了限制返回的结果,我不会调用所有1,000行来设置请求标题。

这是关于此的插件信息:

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,所以当你调用get结果时,你可以设置限制可编程只是为了让它立即运行。

(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知识而被困在这些小事上。我也知道最好将它们设置为异步,但我不能为我的生活找到它,虽然我读的数量少,但我似乎无法理解我的想法在它周围。

0 个答案:

没有答案