Let me explain the problem to you a little, I'm using a 3rd party plugin that runs a rest interface for executing database commands over a postgrest database. In order to limit the returned results so I don't call all 1,000 rows is to set the request header.
This is the plugins information on this:
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-.
I'm writing my own java-script library to handle the rest calls for my project. thus, I'm trying to test limiting the results to just one row returned. However I get the following console error:
Uncaught InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.
Here is my code: note: I am going to add another var so when you make the call to get results you can set the limit programmable just trying to get it to work right now.
(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.");
}
and yes I know it would've been easier to use jquery, however for what i'm doing jquery doesn't work because of its precheck pass it sends to make sure the post is safe to go through isn't compatible with my plugin. This code is very early alpha stages but I've developed a pure js social media site in about 15 hours. I just get stuck on these little things because my lack of js knowledge. I also know that it would be better to set them up as asynchronous but I can't figure that out for the life of me although I've read count less post I can't seem to wrap my mind around it.