beego获取由jquery发送的ajax参数时总是为空的问题

liubaoen 2015-12-01 07:27:15
客户端代码:
function sa_click(obj)
{
var sn=obj.attributes["subject"].value;
var tag=obj.attributes["tag"].value;
$.ajax({
type:"POST",
url:"/querybyid",
data:{id:tag,subject:sn},
dataType: "json",
contentType: "application/json; charset=utf-8",
success:function(result){
json_to_table(result,sn);
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
alert(textStatus);
}
});
}
beego服务器端的几个设置:
router:
beego.Router("/querybyid", &controllers.QueryController{}, "post:QueryById")
QueryController.QueryById:
func (this *QueryController) QueryById() {
rid := this.GetString("id")
beego.Debug("rid", rid)
subject := this.GetString("subject")
beego.Debug("subject", subject)
...
}
在firefox中调试了N次,发现发送的内容为:
request header内容为:
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost/web/query.html
Content-Length: 48
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

request主体内容为:
id=420984199912089040&subject=%E6%80%BB%E5%88%86

但是服务器端调试输出却一直都是如下:
2015/12/01 07:26:55 [queryController.go:44] [D] rid
2015/12/01 07:26:55 [queryController.go:46] [D] subject
值总是空的.

请教高手该如何解决????
...全文
1785 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
liubaoen 2015-12-01
  • 打赏
  • 举报
回复
问题已经解决: ajax中的contentType设置错误导致的。 contentType: "application/json; charset=utf-8", contentType应该理解为传出的参数类型,而不是返回的参数类型, 因为传出的参数并不是json格式的,所以导致解析出错,
liubaoen 2015-12-01
  • 打赏
  • 举报
回复
怎么这么冷清???

2,190

社区成员

发帖
与我相关
我的任务
社区描述
go语言学习与交流版
社区管理员
  • go语言社区
  • Freeman Z
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧