我编写了一个Ajax函数,它应该在框架中用于与with服务器交互。
(自写的)可以用于获取文件和文件夹内容,创建文件和文件夹,并删除它们。
/**
* Performs an Ajax-Request
* @param {string} method HTTP-Method
* @param {string} url
* @param {string} postData optional; HTTP-body
* @param {string} contentType optional; default: "multipart/form-data"
* @param {object} authentication optional; loginCredentials for basicAuth; {"user": "userName", "password": "password"}
* @param {function} callback optional; called, if the operation succeeded;
* Parameter: (string) response-body
* @param {function} errorCallback optional; called, if the operation failed;
* Parameter: 1 = (number) statusCode; (==0, if the given parameters were invalid)
* 2 = (string) statusText; (or errorMessage, if statusCode == 0)
*/
function ajax(method, url, postData, contentType, authentication, callback, errorCallback) {
if(typeof(errorCallback) != 'function'){
errorCallback = function(){}; //Dummy-function to reduce code
}
//Validate the input parameters
if(["GET", "HEAD", "POST", "PUT", "DELETE"].indexOf(method) < 0){
return errorCallback(0, "Invalid method");
}
if(postData && ["HEAD", "POST"].indexOf(method) < 0){ //Other methods don't support postData
return errorCallback(0, "Invalid method for postData");
}
//Get a cross-browser XML-HTTP-Object:
var req = createXMLHTTPObject();
if (!req) {
return errorCallback(0, "Ajax is not supported"); //Oops! Something bad happened
}
req.open(method, url, true); //async = true
//Manipulate header-fields:
//req.setRequestHeader('User-Agent','XMLHTTP/1.0');
if(postData){
if(contentType){
req.setRequestHeader('Content-type', contentType);
}else{
req.setRequestHeader('Content-type', 'multipart/form-data');
}
//key-value pairs with binary data or big payloads:
// multipart/form-data
//key-value pairs with few non-alphanumerical symbols:
// application/x-www-form-urlencoded
//binary data:
// application/octet-stream
}
if(authentication){
req.setRequestHeader("Authorization", "Basic " + btoa(authentication.user + ":" + authentication.password)); //btoa() = encode string to base64
}
req.overrideMimeType("text/plain; charset=utf-8"); //The browser must not execute javascript-files, if they're loaded with this method
//Prepare execution:
req.onreadystatechange = function () {
if (req.readyState != 4) {
return;
}
if (req.status != 200 && req.status != 304) {
return errorCallback(req.status, req.statusText);
}
if(typeof(callback) == 'function'){
callback(req.response);
}
}
if (req.readyState == 4){
return;
}
req.send(postData);
}createXMLHTTPObject()-Method返回XMLHttpRequest-Object或ActiveXObject-Object以实现跨浏览器兼容性。
整个功能应该是跨浏览器兼容的,但不需要支持旧的浏览器,因为框架本身依赖于HTML5。
代码是基于这个例子的
由于我对HTTP协议不太熟悉,所以我有以下问题:
发布于 2014-07-20 18:44:50
对JSONP和CORS的支持是任何现代web框架所必需的。
APIwise,我认为您希望将方法的参数简化为单个对象,类似于jQuery是如何做到的。它提供了一个提供默认设置的机会,并从API的角度简化了它。就目前情况而言,使用它将是痛苦的,因为你需要记住许多论点。一个简单的ajax与您的:
ajax('GET', 'index.html', null, null null function(evt) {
console.log("Success!")
})vs jQUery或类似的:
ajax({method:'GET', url:'index.html'}, success:function(evt) {
console.log("Success!")
})https://codereview.stackexchange.com/questions/57518
复制相似问题