I know it is possible to do an ajax post using jQuery Ajax but how does the call would return a value, text, html or json? Thanks!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s a very good primer on creating web applications in Go here: http://golang.org/doc/articles/wiki/
The example for handling a POST is given in the saveHandler function:
In your case, instead of redirecting, just return a string eg.
.. and (since you’re using jQuery), handle it with a callback as shown in http://api.jquery.com/jQuery.post/:
If you want to return JSON, you’ll need to learn how to Marshal your data, then return that like we return the string above.
Here is a link to to the JSON documentation: http://golang.org/pkg/encoding/json/#Marshal
A good way to get familiar with how to use it is to have a play around on http://play.golang.org, marshalling and unmarshalling structs and printing them out. Here’s an example: http://play.golang.org/p/OHVEGzD8KW