Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将jQueryAjax转换为本机javascript?_Javascript_Jquery_Ajax - Fatal编程技术网

如何将jQueryAjax转换为本机javascript?

如何将jQueryAjax转换为本机javascript?,javascript,jquery,ajax,Javascript,Jquery,Ajax,这是我的ajaxHandler,我想将其转换为本机javascript,即 使用XMLHttpRequest,但我无法理解如何转换` ajaxHandler={ 默认属性:{ 键入:“GET”, url:'index.php/request', 数据类型:“json”, 数据:{}, 成功:空, 错误:函数(数据){ errorHandler.BursError('尝试检索请求的数据时出错,请重试…'); }, 超时:函数(){ errorHandler.batherRor('请求已超时,请检

这是我的ajaxHandler,我想将其转换为本机javascript,即 使用XMLHttpRequest,但我无法理解如何转换`

ajaxHandler={
默认属性:{
键入:“GET”,
url:'index.php/request',
数据类型:“json”,
数据:{},
成功:空,
错误:函数(数据){
errorHandler.BursError('尝试检索请求的数据时出错,请重试…');
},
超时:函数(){
errorHandler.batherRor('请求已超时,请检查您的Internet连接,然后重试…);
}
},
sendRequest:函数(属性){
Paper.giffyLoading.style.display='block';
如果(!attributes.nopopp){
if(attributes.loadmsg){
Controllers.AnimationController.createProgressBarScreen(attributes.loadmsg);
attributes.loadmsg=null;
}
}
$.ajax(属性);
}

}
我提取了Jquery的ajax函数,以便在没有Jquery的情况下工作

并替换
$.ajax(属性)
ajax(属性)

JQuery的ajax函数,不带JQuery:
我提取了Jquery的ajax函数,在没有Jquery的情况下工作

并替换
$.ajax(属性)
ajax(属性)

JQuery的ajax函数,不带JQuery:

你为什么要这样做?请阅读
XMLHttpRequest
:,。也请阅读jqueery AJAX--了解这一点和前面的链接,你会很快找到答案的。我尝试过改变,但没有效果。我希望有人帮我将其转换为本机JS。你为什么要这样做?请阅读
XMLHttpRequest
:,.还可以阅读jqueery AJAX--了解这一点以及前面的链接,你很快就要上路了我试着改变但不起作用我希望有人能帮我把它转换成本机JS当我发送“GET”和“POST”请求时工作正常请更新我如何将查询字符串发送到属性对象的“GET”方法集数据,如:
data:{string:“My string”},
由于内容类型固定,无法处理JSON有效负载。我将从成功函数中删除
|xhr.status==0
,因为这是默认状态、超时时返回的状态以及某些浏览器的错误状态。添加如下内容:
xhr.ontimeout=function(){}取而代之。window.fetch现在可能更好了:当我发送'GET'和'POST'请求时,这工作正常。请更新我将如何将查询字符串发送到属性对象的'GET'方法集数据,如下所示:
data:{string:'My string'},
由于内容类型固定,无法处理JSON有效负载。我将从成功函数中删除
|xhr.status==0
,因为这是默认状态、超时时返回的状态以及某些浏览器的错误状态。添加如下内容:
xhr.ontimeout=function(){}取而代之。window.fetch现在可能更好了:
function ajax(option) { // $.ajax(...) without jquery.
    if (typeof(option.url) == "undefined") {
        try {
            option.url = location.href;
        } catch(e) {
            var ajaxLocation;
            ajaxLocation = document.createElement("a");
            ajaxLocation.href = "";
            option.url = ajaxLocation.href;
        }
    }
    if (typeof(option.type) == "undefined") {
        option.type = "GET";
    }
    if (typeof(option.data) == "undefined") {
        option.data = null;
    } else {
        var data = "";
        for (var x in option.data) {
            if (data != "") {
                data += "&";
            }
            data += encodeURIComponent(x)+"="+encodeURIComponent(option.data[x]);
        };
        option.data = data;
    }
    if (typeof(option.statusCode) == "undefined") { // 4
        option.statusCode = {};
    }
    if (typeof(option.beforeSend) == "undefined") { // 1
        option.beforeSend = function () {};
    }
    if (typeof(option.success) == "undefined") { // 4 et sans erreur
        option.success = function () {};
    }
    if (typeof(option.error) == "undefined") { // 4 et avec erreur
        option.error = function () {};
    }
    if (typeof(option.complete) == "undefined") { // 4
        option.complete = function () {};
    }
    typeof(option.statusCode["404"]);

    var xhr = null;

    if (window.XMLHttpRequest || window.ActiveXObject) {
        if (window.ActiveXObject) { try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } }
        else { xhr = new XMLHttpRequest(); }
    } else { alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest..."); return null; }

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 1) {
            option.beforeSend();
        }
        if (xhr.readyState == 4) {
            option.complete(xhr, xhr.status);
            if (xhr.status == 200 || xhr.status == 0) {
                option.success(xhr.responseText);
            } else {
                option.error(xhr.status);
                if (typeof(option.statusCode[xhr.status]) != "undefined") {
                    option.statusCode[xhr.status]();
                }
            }
        }
    };

    if (option.type == "POST") {
        xhr.open(option.type, option.url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        xhr.send(option.data);
    } else {
        xhr.open(option.type, option.url+option.data, true);
        xhr.send(null);
    }

}