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

2

jQuery 简单地将所有 XmlHttpRequest 调用包装到一个简单易用的库中。在 jQuery 的内部,您将看到创建 XmlHttpRequest 对象的代码。

您可以在此处查看执行此操作的代码:

https://github.com/jquery/jquery/blob/master/src/ajax/xhr.js

使用像 jQuery 这样的框架的好处是它们可以为您处理很多浏览器特性。它们还处理了许多您在编写代码时可能不会考虑的边缘情况。

于 2013-07-05T00:59:24.063 回答
1

使用 jQuery 只是为了易于使用。如果您更喜欢以 javascript 方式执行此操作,请继续执行此操作。

jQuery ajax 调用与您正在执行的操作完全相同,但 jQuery 是一个 javascript 框架,可简化您编写的内容。

于 2013-07-05T01:01:40.683 回答
0

jQuery 是一个 javascript 框架,其中包含与 Native Javascript 相比简化的函数库。其中一项功能是 XMLHttpRequest。

这样我们只需要实现需要实现的功能,而不需要编写传统的代码来设置AJAX系统工作

您可以在此处了解有关 jQuery ajax 的更多信息:

http://api.jquery.com/jQuery.ajax/

于 2013-07-05T01:00:14.767 回答