0

私はこれに対する答えを見つけることができません

私は最近ajaxを学んでいますが、JavaScriptですべてを行う方法を学びました。ここでajaxの質問を泳ぎ回っていますが、ほとんどの人がjqueryを使用しています

だから私は混乱してしまいます。通常の JavaScript を使用するか、jquery を使用する必要がありますか?

違いは何ですか?

これが私の通常のアプローチです

var xmlHttp = createXmlHttpRequestObject();  //you first create the object to this function global

function createXmlHttpRequestObject() //here you instruct the function
{
    var xmlHttp; //here you tell it to use the local variable not the global version of it because this returns to the global with the propper values

    if (window.XMLHttpRequest) //if the "window" or browser is aware of this Object 90% of browsers
    {
        xmlHttp = new XMLHttpRequest(); // if true then the variable is now equal to the heart of ajax

    }
    else //for internet explorer
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlHttp; //we return it back to daddy the global variable this objects does everything
    //this object is everything in ajax for the most part


}

function process() //function that gets called on load of the body
{
    if(xmlHttp) //if its not void or if you can run ajax
    {
        try //try catch statement are sort of required for the heart of things like ajax and server communication
        {
                xmlHttp.open("GET", "bacon.txt", true); //here 1st type of call 2nd from where 3rd 
                //is it true assyscronly or at the same time
                //it does not start the connection to the server, its only sets up settings for the connection

                xmlHttp.onreadystatechange = handleServerResponse; //anytime something changes[propertie]
                //i want to call that function handleserverresponse

                //begin communication
                xmlHttp.send(null); //this is what communicates to the server makesure on ready statechane before this
                //if the server responds you want to make sure everything is taking care of like what function onready state change is it going to call
                //or what to do like open a text file in this case
        } catch(e)
        {
            alert( e.toString() ); //alert the error message as a string on a popup
        }
    }   
}

//handling the server response


function handleServerResponse()
{
    theD = document.getElementById('theD'); //get the div in a var
    if(xmlHttp.readyState==1) //if the connection to the server is established
    { //google crhome may not show this one, some browsers ignore some states
        theD.innerHTML += "Status 1: server connection established<br>";
    }
    else if(xmlHttp.readyState==2) //request received, hey client i am the server and i received your request
    {
        theD.innerHTML += "Status 2: request reveived <br>";
    }
    else if(xmlHttp.readyState==3) //while is doing its thing
    {
        theD.innerHTML += "Status 3: processing request <br>";
    }
    else if(xmlHttp.readyState==4) //your request is finished and ready
    { //it means your response is ready but doesnt guaranteed no trouble

        if(xmlHttp.status==200) //if the status is 200 then is succesufll
        {
            //IF everthing finally good THE GOOD PART
            try //put all your calls on a try statement REMEMBER
            {
                //get the txt file as a string
                text = xmlHttp.responseText; //response text n a normal string
                theD.innerHTML += "Status 4: request is finished and response is finished";
                theD.innerHTML += text;
            }catch (e)
            {
                alert( e.toString() );
            }


        } else //for the other statuses like 404 else somehting went wrong
        {
            alert( xmlHttp.statusText ); //this will give you a status report on the wrong
        }
    }

}
4

3 に答える 3