This is a creation in Article, where the information may have evolved or changed.
Recently replaced some pages of my blog with angular. Results tragic discovery, POST request to Revel, Revel Paramsfilter parse not coarse to parameters.
Looking at the request information, we find that the POST request of jquery and angular is somewhat different.
jquery's content type is application/x-www-form-urlencoded, which will stitch the post parameters to the URL, formatted like Foo=bar&baz=moe.
In angular, the default content type is Application/json, and the data is sent in JSON format.
However, in the Revel Params.go, there is no parameter processing in the JSON format request.
In the author's words, this JSON processing is useless, and in the controller with Encoding/json processing is only a few lines of code things. So, there is no so ...
The solution to this problem, there are many, can be resolved from the angular level, the angular post request also follow the JQuery method to make some changes, as follows:
http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
Https://github.com/petersirka/total.js/issues/26
can also be resolved from the Revel server.
Https://github.com/robfig/revel/issues/97
The code on this page is modified as follows:
func (c *Task) NewTask() revel.Result { decoder := json.NewDecoder(c.Request.Body) var content ToDoContent if err := decoder.Decode(&content); err != nil { print(">>>err:", err) } else { print(">>>>content:", content.Content) } json.Marshal(content) return c.RenderJson(content)}
Although this code is really not much, but the instant feeling than rails is weak ...
Incidentally, if it is jquery request, also need to change a little bit, otherwise, revel the same parsing not thick.
Jsonstringify the post in jquery.
For specific reference here https://github.com/robfig/revel/issues/126
$.ajax({ type:"POST", url:"/Application/Index", data:JSON.stringify({ name: "John", time: "2pm" }), contentType:"application/json", dataType:"json"} )