NutzCN Logo
问答 nutzboot 中使用vue-resource.js请求数据的时候,Controller方法获取不到数据,下面是代码?
发布于 2221天前 作者 小苍蝇 2554 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

var app = new Vue({
el: '#app',
data: {
message: ''
},
methods: {
post: function() {
this.$http.post("${base}/beetl/vue", {name:"Vue",age:11}).then(function(res) {
this.message = res.body;
console.log("post请求,"+JSON.stringify(res.body));
}, function(res) {
console.log(res.status)
});
}
},
mounted: function() { //自动执行
this.post();
}
})

@At("/vue")
@Ok("json:full")
public NutMap getVue(@Param("name")String name,@Param("age")int age) {
NutMap obj = new NutMap();
obj.put("name", name);
obj.put("age", age);
return obj;
}

23 回复

忘了用日志贴代码了! 再来一次

var app = new Vue({
  	el: '#app',
  	data: {
    	message: ''
  	},
  	methods: {
		post: function() {
			 this.$http.post("${base}/beetl/vue", {name:"Vue",age:11}).then(function(res) {
				 this.message = res.body;
				 console.log("post请求,"+JSON.stringify(res.body));
			}, function(res) {
				console.log(res.status)
			}); 
		}
	},
	mounted: function() { //自动执行
		this.post(); 
	} 
})
	@At("/vue")
    @Ok("json:full")
    public NutMap getVue(@Param("name")String name,@Param("age")int age) {
		NutMap obj = new NutMap();
		obj.put("name", name);
		obj.put("age", age);
		return obj;
	}

在getVue方法上加上
@AdaptBy(type = JsonAdaptor.class)

解决了,还有一个问题

后台得到的age值字符串是 “v2.5“, 我前端传的是“v2.5.11“啊

		put: function() {
			this.$http.put("${base}/beetl/vue/Vue/v2.5.11").then(function(res) {
				this.message = res.body;
				console.log("get请求,"+JSON.stringify(res.body));
			}, function(res) {
				console.log(res.status)
			});
		},
	@At("/vue/?/?")
    @Ok("json:full")
    public NutMap getVue(String name,String age) {
		NutMap obj = new NutMap();
		obj.put("name", name);
		obj.put("age", age);
		return obj;
	}

你看下浏览器开发者工具中的network,看看实际调用的完整URL是什么
我的认知告诉我,不应该会出现这问题的!吃饭URL中传递的就是v2.5.11

更正:除非URL中传递的就是v2.5

我调式了下,下面你看看

PUT 
XHR 
http://localhost:8080/iads/beetl/vue/Vue/v2.5.11 [HTTP/1.1 200 OK 15ms]
响应头
Cache-Controlno-cacheContent-Typeapplication/json;charset=utf-8DateWed, 28 Mar 2018 03:37:04 GMTServerJetty(9.4.9.v20180320)Transfer-EncodingchunkedX-Powered-Bynutz/1.r.66-SNAPSHOT <nutzam.com>

请求头
Acceptapplication/json, text/plain, */*Accept-Encodinggzip, deflateAccept-Languagezh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3Cache-Controlmax-age=0Connectionkeep-aliveContent-Length0Content-Typeapplication/json;charset=utf-8Cookiesid=292mg351vkhl4o7n5m1llmu6cv; locale=zh; sid=3i1ni9u0ksionrhd2b4ka6a1p7Hostlocalhost:8080Refererhttp://localhost:8080/iads/beetlUser-AgentMozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0X-Requested-WithXMLHttpRequest
打印的值
get请求,{"name":"Vue","age":"v2.5"}

有一种可能,是不是点号被看作是扩展分隔符号了
如果是,不清楚nutz下是否有和springmvc一样的注释 @RequestMapping(value = "/vue/{keyword:.+}")

更奇怪的,这样改,age既然变成2了,我传的是2.5啊

put: function() {
			this.$http.put("${base}/beetl/vue/Vue/2.5").then(function(res) {
				this.message = res.body;
				console.log("get请求,"+JSON.stringify(res.body));
			}, function(res) {
				console.log(res.status)
			});
		},

那咋搞,我这个项目都是计算传值

简单的就是传递json参数,不使用RESTful分格呗
用你第一次po出来的代码
在getVue方法上加上
@AdaptBy(type = JsonAdaptor.class)
就可以接收了参数了

那就没错啦,最后一个点被当成扩展符号了

关键是,我们是第二期的项目,在第一期的上面改,后端保留原来的接口,前端全部换掉,原来用的ajax是可以的

var app = new Vue({
 el: '#app',
 data: {
 message: ''
 },
 methods: {
 post: function() {
 this.$http.post("${base}/beetl/vue", {name:"Vue",age:11}).then(function(res) {
 this.message = res.body;
 console.log("post请求,"+JSON.stringify(res.body));
 }, function(res) {
 console.log(res.status)
 });
 }
 },
 mounted: function() { //自动执行
 this.post();
 }
})

@At("/vue")
 @Ok("json:full")
@AdaptBy(type = JsonAdaptor.class)
 public NutMap getVue(@Param("name")String name,@Param("age")int age) {
 NutMap obj = new NutMap();
 obj.put("name", name);
 obj.put("age", age);
 return obj;
 }

@qq_d1ad4e17 那这就不知道怎么帮你了,只能等wendal来解答了,我们前后端用的都是json来通讯
我帮你@wendal下,哈哈

好的好的! 多谢多谢 O(∩_∩)O

ajax和vue都不行,我刚刚测试了一下,age都是2,看能不能帮下,解决一下

put: function() {
			this.$http.put("${base}/beetl/vue/Vue/2.5").then(function(res) {
				this.message = res.body;
				console.log("get请求,"+JSON.stringify(res.body));
			}, function(res) {
				console.log(res.status)
			});
		},
		ajax: function() {
			$.ajax({
				type:"get",
				url:"${base}/beetl/vue/Vue/2.5",
				async:true
			}).then(function(data){
				console.log("ajax get请求,"+JSON.stringify(data));
			},function(){
				console.log("请求错误");
			});
		},
url:"${base}/beetl/vue/Vue/2.5/",

末尾加个反斜杠.

不是转义,是当成后缀了

@wendal 噢噢! soga! 多谢多谢

添加回复
请先登陆
回到顶部