Ajax asynchronous transfer value summary

Ajax asynchronous transfer value

The data transfer from the foreground to the background:

 

1 : The get way, the parameters in the link, conducted with traditional values "?."

 

Example:

// reception by value method    

// This method is called trigger ajax

 function  testAjax (yourData) {

     $.ajax({

         type: "get", // initiate a request to get way

         url: "? / yourUrl yourDataName = " + yourData, // your request parameters to splice a question mark url in parameter passing

         success(data) {

             // data for the return value

             After the success callback method //

         }

     })

 }

 

 

// backstage access value method

@RequestMapping("/yourUrl")

@ResponseBody

// @ RequestParam ( "yourData") is essential, because he specified parameter name in the link

public String yourUrl(@RequestParam("yourData") String yourData) {

    System.out.println(yourData);

    // return value can be freely defined

    return "SUCCESS";

}

 

2 : the splice in the link parameters directly , the background is transmitted via the placeholder

// Reception

function addTec(tecId) {

 $.ajax({

cache : true,

type : "get",

url : "/factory/tec/listOrderNumByMatId/"+tecId,

async : false,

success : function(data) {

    

}

});

}

 

// background

@GetMapping("/factory/tec/listOrderNumByMatId/{tecId}")

String add(Model model, @PathVariable("tecId") Long tecId) {

System.out.println(tecId);

}

 

3 : By post submission the form data in the form of a sequence is transmitted back to the background.

 

// reception by value method

function  testAjax () {

   $.ajax({

        type: "post", // initiate a request to post way

        url: "/ yourUrl", // your link request

        data:. $ ( "# myForm") serialize (), // id of form data to be serialized and transmitted to the back of myForm

        success(data) {

               // data for the return value

               After the success callback method //

           }

       })

   }

 

 

Background is generally performed by a receiving entity class

// backstage access value method

@RequestMapping("/yourUrl")

@ResponseBody

// here I am assuming that you form data corresponding to the User entity class

public String yourUrl(User user) {

     System.out.println(user.toString());

     return "SUCCESS";

 }

 

4: pass through the data value to the background by the ajax parameter to map (key-value) manner.

 

// reception by value method

function  testAjax () {

    $.ajax({

        type: "post", // initiate a request to post way

        url: "/ yourUrl", // your link request

        data: {// submit data

            "Username": "admin", // the former name for the field, the latter data

             "password": "admin"

         },

           success(data) {

               // data for the return value

               After the success callback method //

           }

       })

   }

 

 

 

// backstage access value method

@RequestMapping("/yourUrl")

@ResponseBody

// here I am assuming that you form data corresponding to the User entity class

public String yourUrl(@RequestParam("username") String username, @RequestParam("password") String password) {

       System.out.println("username="+username+";password="+password);

   return "SUCCESS";

}

Guess you like

Origin www.cnblogs.com/liujianjun8181/p/11316178.html