angularjs中$http服务的POST用法实例
一、为鼠标点击事件添加响应
scope.myClick = function() { $http({ method: "POST", url: "http://localhost:8088/test", data: "123456", headers: { Content-Type: application/json }, }).then( function success(){ //响应成功的处理方法体 },function error(){ //响应错误的处理方法体 }); };
需要注意的是:要在对应的controller中添加$http。
例如:
app.controller("MainController",function($scope,$http){ ... })
二、在IDEA中新建一个简单的springboot测试项目
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 "已接收请求"; } }
一般默认端口号为8080,由于该端口号与本来项目中的端口号冲突,故修改端口号为8088。
方法:在resources包下新建一个application.properties文件,直接写入 server.port=8088即可。
点击运行后,显示下列信息即创建成功。
三、测试
运行包含$http代码的项目,点击对应按钮,若测试项目中显示1111111则表示通信成功。