vue-jsonp 如何使用
前端
npm安装
npm install vue-jsonp
在 main.js 中添加
import { VueJsonp } from 'vue-jsonp'
Vue.use(VueJsonp)
在组件中使用
this.$jsonp('后台请求地址',{
data: JSON.stringify(要传的参数)
}).then( data => {
// 成功
}).catch( error => {
// 失败
})
后端
@RequestMapping("/test")
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 设置返回数据格式
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; chaset=utf-8");
// 将返回数据转为json字符串
String resultJson = JSON.toJSONString(返回数据);
// 获取回调名称
String callback = request.getParameter("callback");
// 返回数据
PrintWriter printWriter = response.getWriter();
printWriter.write(callback + "(" + resultJson + ")");
}
正文到此结束
- 本文标签: vue Spring Boot
- 版权声明: 本站原创文章,于2022年05月05日由丙火发布,转载请注明出处