How can I re-write this code without jQuery?

2020-05-29 06:08发布

How can I rewrite this code without using jQuery? I need to do it in a mobile app where I can't use 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);
                }    

        }); 

5条回答
Juvenile、少年°
2楼-- · 2020-05-29 06:46

For the Ajax request itself, have a look at the XMLHttpRequest object (special treatment for IE).

To parse the JSON response (dataType: 'json'), use JSON.parse (you might need the json2.js library).

查看更多
Luminary・发光体
3楼-- · 2020-05-29 06:56

You can try this:

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));

As for how to do more stuff with only javascript without jQuery library take a look at W3Schools AJAX Tutorial or Mozilla - Using XMLHttpRequest

And as duri said, you will have to find a way to convert dataObject to string, as not all browsers support JSON object.

查看更多
【Aperson】
4楼-- · 2020-05-29 06:58

jquery is already javascript. It's a library, entirely written in javascript. So you can use it within every javascript project. If you want to write a rest client yourself, you might look at this article

查看更多
▲ chillily
5楼-- · 2020-05-29 06:59

try this

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)
    }
}
查看更多
你好瞎i
6楼-- · 2020-05-29 06:59

You may also consider using jQuery Mobile edition - very light - 17KB minified. 1.0 is alpha now, but I think it will be more stable and portable than self-baked code.

查看更多
登录 后发表回答