Javascript 如果没有jQuery,如何重新编写此代码?

Javascript 如果没有jQuery,如何重新编写此代码?,javascript,jquery,ajax,Javascript,Jquery,Ajax,如果不使用jQuery,如何重写此代码?我需要在一个不能使用jQuery的移动应用程序中完成这项工作 $.ajax({ type: "POST", url:"../REST/session.aspx?_method=put", data: JSON.stringify(dataObject, null,4), cache: false, dataType: "json",

如果不使用jQuery,如何重写此代码?我需要在一个不能使用jQuery的移动应用程序中完成这项工作

 $.ajax({
            type: "POST", 
            url:"../REST/session.aspx?_method=put",
            data: JSON.stringify(dataObject, null,4),
            cache: false,
            dataType: "json",
            success: onSuccessLogin,
            error: function (xhr, ajaxOptions){
                    alert(xhr.status + " : " + xhr.statusText);
                }    

        }); 

jquery已经是javascript了。这是一个完全用javascript编写的库。因此,您可以在每个javascript项目中使用它。如果您想自己编写rest客户机,您可以查看Ajax请求本身,查看对象()

要解析JSON响应(
dataType:'JSON'
),请使用(您可能需要)。您可以尝试以下方法:

var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
    // What you want to do on failure
    alert(xmlhttp.status + " : " + xmlhttp.responseText);
  }
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    // What you want to do on success
    onSuccessLogin();
  }
}

xmlhttp.open("POST", "../REST/session.aspx?_method=put", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Cache-Control", "no-cache"); // For no cache
xmlhttp.send("data=" + JSON.stringify(dataObject, null,4));
至于如何只使用javascript而不使用jQuery库来做更多的事情,请看一下

正如duri所说,您必须找到将dataObject转换为字符串的方法,因为并非所有浏览器都支持JSON对象。

试试这个

var xmlHttp = createXmlHttpRequestObject();
//retrieves the xmlHttpRequest Object
function createXmlHttpRequestObject()
{
    //will store the refrence to the xmlHttpRequest Object
    var xmlHttp;
    //if running in internet explorer version below 7
    if(window.ActiveXObject)
    {
            try{
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                xmlHttp = false;
            }
    }
    else
    {
        try{
            xmlHttp = new XMLHttpRequest();
        }
        catch(e){
            xmlHttp = false;
        }
    }
    if(!xmlHttp)
        alert("Error Creating the xmlHttpObject");      
    else
        return xmlHttp;
}

function callThisFunction()
{
    if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
    {

            var queryString = JSON.stringify(dataObject, null,4);

            var url = "../REST/session.aspx?_method=put";

            xmlHttp.open("POST", url, true);

            //Send the proper header information along with the request
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.setRequestHeader("Content-length", queryString.length);
            xmlHttp.setRequestHeader("Connection", "close");

            xmlHttp.onreadystatechange = checkHandleServerResponse;         
            xmlHttp.send(queryString);  
    }
}


function checkHandleServerResponse()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {
            var JSONFile = 'jsonFormatData = '+xmlHttp.responseText;
            eval(JSONFile);
            if(jsonFormatData.description == 'Success')
            {
                //showDialog('Success','Login Successful','success',3);
                location.href='default.aspx';   
                //setTimeout(function(){location.href='myGrupio.php';},3000);
            }
            else
                showDialog('Error','You Need a Valid Login.','error',3);
        }
        else if(xmlHttp.status == 400)
        {
            setTimeout( function()
                        {
                            alert('you have error');
                        },3000);
        }
        else 
            alert("Error Code is "+xmlHttp.status)
    }
}

<>你也可以考虑使用非常轻-17KB的版本。1.0现在是alpha,但我认为它将比自焙代码更加稳定和可移植。

jQuery是javascript。。。您想停止使用jQuery框架并用纯javascript编写它吗?如果是,请说明原因;)因为我正在开发一个移动网络应用程序。一些低配置的手机不支持jQuery,所以我试着用java脚本编写。最简单的方法是查看jQuery.ajax函数的源代码。然后你应该搜索XMLHttpRequest,有很多好东西。@Liviu t:“最简单的方法是查看jQuery.ajax函数的源代码”显然,您从未看过jQuery源代码Confluously没有半点说出来。尽管他用了一些措辞,但很清楚他真正想问的是什么。我想用java脚本调用web服务。没有jQuery-od-ajax。因为要使用异步javascript调用rest接口,所以必须使用ajax。ajax只是异步JavaScript和XML的简写。因此,它和jquery一样,都是100%纯javascript。但是,当您遇到与其他库的兼容性问题时,不使用像jquery这样的库是有意义的……并非每个浏览器都支持JSON对象。我还调用onsuccesslogin方法。我在哪里可以调用该函数?@duri我不确定所有浏览器中是否都有默认的JSON对象或类似的东西,所以Durai必须找到一种方法将dataObject转换为字符串。但是,由于我不知道dataObject看起来是什么样子,所以我也无能为力。@Durai当xmlhttp.readyState==4(完成)和xmlhttp.status==200(确定)时,您可以从xmlhttp.onreadystatechange函数内部调用onsuccesslogin。