Created
August 10, 2019 11:31
-
-
Save Nasirinezhad/79bdfc4294f490e611217a94ff3912b6 to your computer and use it in GitHub Desktop.
[Qt-qml] A simple javascript Rest http Requst(Get-Post-Put-Delete)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Request = function (options) { | |
var http = { | |
"xhr": new XMLHttpRequest(), | |
"URLbase": '', | |
"method": 'GET', | |
"userAgent": null, | |
"success": null, | |
"fail": null, | |
"callback": function () { | |
if (http.xhr.readyState !== 4) { | |
return | |
} | |
if (http.xhr.status === 200 && http.success) { | |
http.success(JSON.parse(http.xhr.responseText)) | |
} else if (http.fail) { | |
http.fail(http.xhr.status, http.xhr.responseText) | |
} | |
}, | |
"setHeaders": function (headers) { | |
for (var hkey in headers) { | |
if (headers.hasOwnProperty(hkey)) { | |
this.xhr.setRequestHeader(hkey, headers[hkey]) | |
} | |
} | |
}, | |
"request": function (uri, async, user, password) { | |
this.xhr.open(uri.method, uri.url, async, user, password) | |
this.xhr.onreadystatechange = this.callback | |
if (uri.headers) { | |
this.setHeaders(uri.headers) | |
} | |
var methods = ['GET', 'COPY', 'HEAD', 'OPTIONS', 'PURGE'] | |
if (methods.indexOf(uri.method) > -1) { | |
this.xhr.send(null) | |
} else { | |
this.xhr.send(uri.data) | |
} | |
}, | |
"then": function (callback) { | |
if (this.xhr.readyState === 4) { | |
callback(JSON.parse(this.xhr.responseText)) | |
} else { | |
this.success = callback | |
} | |
return this | |
}, | |
"catch": function (callback) { | |
if (this.xhr.readyState === 4) { | |
callback(this.xhr.status, this.xhr.responseText) | |
} else { | |
this.fail = callback | |
} | |
return this | |
}, | |
"get": function (url) { | |
this.request({ | |
"method": 'GET', | |
"url": this.URLbase + url | |
}, true) | |
return this | |
}, | |
"post": function (url, data) { | |
this.request({ | |
"method": 'POST', | |
"headers": { | |
"Content-type": 'application/json' | |
}, | |
"url": this.URLbase + url, | |
"data": JSON.stringify(data) | |
}, true) | |
return this | |
}, | |
"put": function (url, data) { | |
this.request({ | |
"method": 'PUT', | |
"headers": { | |
"Content-type": 'application/json' | |
}, | |
"url": this.URLbase + url, | |
"data": JSON.stringify(data) | |
}, true) | |
return this | |
}, | |
"del": function (url, data) { | |
if (typeof data !== 'object') { | |
data = { | |
"id": data | |
} | |
} | |
this.request({ | |
"method": 'DELETE', | |
"headers": { | |
"Content-type": 'application/json' | |
}, | |
"url": this.URLbase + url, | |
"data": JSON.stringify(data) | |
}, true) | |
return this | |
} | |
} | |
for (var op in options) { | |
http[op] = options[op] | |
} | |
return http | |
} | |
/* Simple example | |
var connection = new Rest.Request({ | |
"URLbase": 'http://127.0.0.1/api/' | |
}) | |
connection.put('Table', { | |
"id": 12, | |
"name": "name", | |
"date": "now" | |
}).then(function (JsonObject) { | |
textFilde1.text = JsonObject.stat | |
}).catch(function (err) { | |
console.log(err) | |
}) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment