Implementation:APIJSON APIJSON Demo JSONRequest JS
| Knowledge Sources | |
|---|---|
| Domains | API_Design, HTTP_Client, Frontend |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete tool for constructing and sending HTTP POST requests to APIJSON server endpoints provided by the APIJSON Demo frontend.
Description
The JSONRequest JavaScript module wraps XMLHttpRequest to send APIJSON-formatted JSON requests to the server's generic CRUD endpoints (/get, /post, /put, /delete, /head, /gets, /heads). It handles URL construction, JSON formatting, debug alerting, and response parsing. It defines base URL constants and the seven APIJSON method names.
Usage
Import this module when you need to send APIJSON requests from a browser-based JavaScript client to the APIJSON Demo server.
Code Reference
Source Location
- Repository: APIJSON_APIJSON_Demo
- File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson/JSONRequest.js
- Lines: 1-552
Also duplicated at static/unit/apijson/JSONRequest.js (481 lines).
Signature
var URL_BASE = "http://localhost:8080";
var URL_GET = URL_BASE + "/get";
var URL_POST = URL_BASE + "/post";
var URL_PUT = URL_BASE + "/put";
var URL_DELETE = URL_BASE + "/delete";
var APIJSON_METHODS = ["get", "head", "gets", "heads", "post", "put", "delete"];
function request(url, json, notAlertRequest, onreadystatechange) { ... }
function get(json, notAlertRequest, onreadystatechange) { ... }
function post(json, notAlertRequest, onreadystatechange) { ... }
function put(json, notAlertRequest, onreadystatechange) { ... }
function del(json, notAlertRequest, onreadystatechange) { ... }
function format(json) { ... }
Import
var JSONRequest = require("./JSONRequest");
// or via script tag in browser
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | String | Yes | APIJSON endpoint URL |
| json | JSONObject | Yes | APIJSON request body |
| notAlertRequest | Boolean | No | Suppress debug alerts |
| onreadystatechange | Function | No | XHR callback |
Outputs
| Name | Type | Description |
|---|---|---|
| request() returns | JSONObject | Parsed JSON response from server |
| get/post/put/del | Function | Convenience wrappers for request() |
Usage Examples
Send a GET Request
var JSONRequest = require("./JSONRequest");
// Query User table
var request = {
"User": {
"id": 82001
}
};
JSONRequest.get(request, true, function(response) {
console.log(response);
// { "code": 200, "User": { "id": 82001, "name": "Test", ... } }
});
Send a POST Request
// Insert a new user
var newRecord = {
"User": {
"name": "NewUser",
"sex": 1
}
};
JSONRequest.post(newRecord, true, function(response) {
console.log(response);
// { "code": 200, "User": { "id": 12345, ... } }
});