JQuery handles parsing of returned data of instances in XML format

  • 2020-03-30 00:03:25
  • OfStack

In this sample program, I'll use the $.ajax() method, or the $.get() method, but I think $.ajax() is better, the code is easier to understand, and it's not too complicated.

//Defines a method for verifying user names
function verify(){
    //First test the page by pressing the button to call this method
    //Use the javascript alert method to display a probe box
    //Alert (" button clicked!! ") );
    //1. Get the contents of the text box
    //Document. The getElementById (" userName ");   Dom way
    //Jquery's way of finding nodes, the # in the parameter plus the id attribute value can find a node.
    //Jquery methods return jquery objects, and you can continue to execute other jquery methods on them
    var jqueryObj = $("#userName");
    //Gets the value of the node
    var userName = jqueryObj.val();
    //2. Send the data in the text box to the servlets in the server segment
    //In javascript, a simple method of object definition
    var obj = {name:"123",age:20};
    //Encapsulation of get requests using the XMLHTTPrequest object jquery
        type: "POST",            //HTTP request mode
        url: "AJAXXMLServer",    //Server segment url address
        data: "name=" + userName,           //Data sent to the server segment
        dataType: "xml",  //Tells JQuery the format of the data returned
        success: callback  //Define the callback function to call when the interaction is complete and the server returns the data correctly

Callback function:

//The callback function
function callback(data) {
//      Alert (" server segment data is back!!" );
    //3. Receive the data returned from the server
    //The data in the dom object data needs to be parsed out
    //You first need to convert the dom object to the JQuery object
    var jqueryObj = $(data);
    //Get the message node
    var message = jqueryObj.children();
    //Get text content
    var text = message.text();
    //4. Dynamically display the data returned by the server segment on the page
    //Find the node that holds the result information
    var resultObj = $("#result");
    //Dynamically changes the content of the div node in the page

Related articles: