Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Guillaume Royer
jio
Commits
d2c5f881
Commit
d2c5f881
authored
11 years ago
by
Tristan Cavelier
Browse files
Options
Download
Email Patches
Plain Diff
util.ajax updated to give jquery like object parameter
parent
08666f6d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
14 deletions
+31
-14
src/jio/core/util.js
src/jio/core/util.js
+31
-14
No files found.
src/jio/core/util.js
View file @
d2c5f881
...
...
@@ -319,26 +319,43 @@ function readBlobAsBinaryString(blob) {
exports
.
util
.
readBlobAsBinaryString
=
readBlobAsBinaryString
;
/**
* Send request with XHR and return a promise.
* Send request with XHR and return a promise. xhr.onload: The promise is
* resolve when the status code is lower than 400 with the xhr object as first
* parameter. xhr.onerror: reject with xhr object as first
* parameter. xhr.onprogress: notifies the xhr object.
*
* @param {String} method The request method
* @param {String} url The url
* @param {Any} [data] The data to send
* @param {Function} [before_send] A function called just before send request.
* The first parameter of this function is the XHR object.
* @param {Object} param The parameters
* @param {String} [param.type="GET"] The request method
* @param {String} [param.dataType=""] The data type to retrieve
* @param {String} param.url The url
* @param {Any} [param.data] The data to send
* @param {Function} [param.beforeSend] A function called just before send
* request. The first parameter of this function is the XHR object.
* @return {Promise} The promise
*/
function
ajax
(
method
,
url
,
data
,
before_send
)
{
var
xhr
=
new
XMLHttpRequest
(),
deferred
=
new
Deferred
();
xhr
.
open
(
method
,
url
,
true
);
xhr
.
responseType
=
"
blob
"
;
xhr
.
onload
=
deferred
.
resolve
.
bind
(
deferred
);
function
ajax
(
param
)
{
var
k
,
xhr
=
new
XMLHttpRequest
(),
deferred
=
new
Deferred
();
xhr
.
open
(
param
.
type
||
"
GET
"
,
param
.
url
,
true
);
xhr
.
responseType
=
param
.
dataType
||
""
;
if
(
typeof
param
.
headers
===
'
object
'
&&
param
.
headers
!==
null
)
{
for
(
k
in
param
.
headers
)
{
if
(
param
.
headers
.
hasOwnProperty
(
k
))
{
xhr
.
setRequestHeader
(
k
,
param
.
headers
[
k
]);
}
}
}
xhr
.
onload
=
function
(
e
)
{
if
(
e
.
target
.
status
>=
400
)
{
return
deferred
.
reject
(
e
);
}
deferred
.
resolve
(
e
);
};
xhr
.
onerror
=
deferred
.
reject
.
bind
(
deferred
);
xhr
.
onprogress
=
deferred
.
notify
.
bind
(
deferred
);
if
(
typeof
before
_s
end
===
'
function
'
)
{
before
_s
end
(
xhr
);
if
(
typeof
param
.
before
S
end
===
'
function
'
)
{
param
.
before
S
end
(
xhr
);
}
xhr
.
send
(
data
);
xhr
.
send
(
param
.
data
);
return
deferred
.
promise
();
}
exports
.
util
.
ajax
=
ajax
;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment