Example of POST usage of $http service in angularjs

1. Add a response to the mouse click event

scope.myClick = function() {
            $http({
                method: "POST",
                url: "http://localhost:8088/test",
                data: "123456",
                headers: { 'Content-Type': 'application/json' },
            }).then(
                function success(){
                      //响应成功的处理方法体
                },function error(){
                      //响应错误的处理方法体
                });
        };

It should be noted that: $http should be added to the corresponding controller.

E.g:

app.controller("MainController",function($scope,$http){
       ...
})

2. Create a simple springboot test project in IDEA

package com.example.demonew.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class test {

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String hello()
    {
        System.out.println("1111111");
        return "已接收请求";
    }
}

Generally, the default port number is 8080. Because this port number conflicts with the port number in the original project, the port number is changed to 8088.

Method: Create a new application.properties file under the resources package and directly write server.port=8088.

After clicking Run, the following information is displayed and the creation is successful.

Three, test

Run the project containing the $http code and click the corresponding button. If 11111111 is displayed in the test project, it means the communication is successful.

 

Guess you like

Origin blog.csdn.net/weixin_39006917/article/details/96422764