The difference between jquery and angular Ajax requests

Source: Internet
Author: User
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"} )
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.