duanji1902
2018-07-21 10:35读取Json Form数据golang
I am sending form data in JSON & serialize format to golang server using ajax. I am not able to read those data.
I am using kataras/iris
golang framework.
Below is my code -
(function ($) {
$.fn.serializeFormJSON = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
})(jQuery);
var Contact = {
sendMessage: function() {
return m.request({
method: "POST",
url: "/send/message",
data: JSON.stringify(jQuery('#contact-form').serializeFormJSON()),
withCredentials: true,
headers: {
'X-CSRF-Token': 'token_here'
}
})
}
}
<!-- Data looks like below, what is sent -->
"{\"first_name\":\"SDSDFSJ\",\"csrf.Token\":\"FjtWs7UFqC4mPlZU\",\"last_name\":\"KJDHKFSDJFH\",\"email\":\"DJFHKSDJFH@KJHFSF.COM\"}"
And I am trying to fetch the data from server using below code -
// Contact form
type Contact struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
}
contact := Contact{}
contact.FirstName = ctx.FormValue("first_name")
contact.LastName = ctx.FormValue("last_name")
contact.Email = ctx.FormValue("email")
ctx.Writef("%v", ctx.ReadForm(contact))
My all data is blank, How to grab the data? I am using https://github.com/kataras/iris golang framework.
</div>
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- 如何从golang项目文件获取图像路径
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 1个回答
- 如何在Docker上获取AWS凭证和访问S3
- docker
- 2个回答
- 读取Json Form数据golang
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 1个回答
- 在Golang中获取POST参数,并将标头作为application / json
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 2个回答
- 正确创建一个JSON文件并从中读取
- json
- 1个回答
换一换