I know that I can submit form information from a web page by using the #MultipartForm annotation to bind a POJO to a form. (See How do I do a multipart/form file upload with jax-rs?).
What I would like to do, however, is make a Restful call that returns a POJO containing values, and have those values fill in the appropriate form values, which can then be edited and submitted by the user. I know I'd probably need to use JavaScript to first make the rest call, but at that point, is there a way I can use the result to fill in the form?
I could have my rest call return JSON representing the form and use those values to fill in the form with JavaScript, but it would be cool if this could happen automatically, similar to form posting.
Thanks!
Related
Let's say I have an input box, on the client-side, I want to access the value of this input box and check if it exists as a record in a model. If so, I want the data to be shown. However, I want this to be done without clicking a submit button/reloading the page.
Can someone show me some sample code?
Sample code is very much frowned upon in these parts, I can give you an outline of what you could do. Use jquery to get the value of the input box, and submit this using an ajax request to some url. Map that url to a controller action that checks to see if that record exists in the db, and return some json that indicates whether it does or not. Then in your javascript, when the data is returned, you can display that to the user.
I've got some Javascript which gets the HTML for a form from the server via an XMLHttpRequest, and displays the form on the page.
What I'd like to be able to do is extract the data which would be sent via POST if the form was submitted, then send it via another XHR. The tricky bit is that the form could contain different elements of different types.
Is there a way of doing this without having to inspect each element on the form, determine its type, manually extract the data based on the type, and build it all into a query string?
Is there a way of doing this without having to inspect each element on the form, determine its type, manually extract the data based on the type, and build it all into a query string?
No, there isn't, but there are prewritten libraries that have the code for that already written. e.g. jQuery serialize
Note that if you want to make a POST request, then the data should go in the request body, not the query string.
I'm new to JavaScript, and trying to figure out the canonical way to do the following.
I have a form with some checkboxes and a selector.
Let's say the checkboxes are styles of music and the selector is for people's names.
I'd like the user to be able to select the styles of music for multiple people and then submit the form with all of the data.
For example, the user might first check off Classical, Jazz, Rock, and Pop and choose "Joe", then select Jazz, Pop, Country, and Electronica and choose "Jane". So there would have to be two different buttons for "submit person" and "submit form".
I would like to:
Have a list of the names and their chosen styles populate below the form, for feedback
Allow the user to use the form as much as they want, and then submit all the data at the end
I get the feeling that using jquery and JSON is perfect for this, but I'm not sure what search terminology to use to figure out how to do this.
If it matters, the form will be processed by a Django view in Python.
You can achieve this by using AJAX for submit person. Your work flow should go like this:
User selects Joe and the corresponding styles of music.
User hits 'Submit Person'. On this event, encode the name of the person (Joe) and the styles of music selected into a JSON object and pass it to your back end script via an AJAX (jquery ajax() ) request.
The server side script with do whatever processing it needs to. When it finishes, you AJAX calls' success handler will be invoked. At this point, you can probably remove 'Joe' so the user knows the submission was successful and does not submit for Joe again.
Wash, rinse, repeat for all other people in your form.
PS - when you pass information to the back-end via AJAX, you do not have to encode it as JSON. You can either send it as a standard POST request. To encode a Javascript objects into JSON, use JSON.stringify()
The above is one way of doing it, however it won't work like you asked in your question (keep collecting data - submit at once), To work it that way, everytime the user hits 'Submit Person', add the data to a Javascript object, but do not submit it. The submitted data will keep building up in a JS object.
Finally, when the user hits 'Submit form', stringify the data and submit it.
How about using django form-wizard.. Could it be enough? https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/
I am trying to implement GSA(Google Search Appliance) in my app. I want to use the REST(JSON) call that the GSA provides. The point for this question is that, the GSA needs a POST request in order to return the JSON response.
Now when I made a new dummy HTML page with a form and make a POST request with parameters I get a successful response(JSON)
But, when I try using the $.post(...) method to send a POST request to the URL I am not getting the actual response, but some error page.
I just wanted to know is there a difference between a standard submit and an ajax form submit. If yes, is there any workaround for this situation.
Please Help. Thanks in Advance.
If you want to submit the form through ajax but in the conventional way, You should have a look at jquery form plugin . Just make your submit button to type button and on click submit your form thorugh .ajaxSubmit(). I think this will solve your problem.
GSA search protocol is based on HTTP GET. All search parameters need to be passed in via query string. Also, out of box, GSA only returns either HTML or XML results. You could apply an xslt that transforms xml to JSON -- but I'm yet to find one that works really well (i.e., I've found a couple but they don't return valid JSON in all instances).
I'm struggling to find the best way to display validation messages after a form which has been generated via ajax is submitted.
Let me explain better:
I've got a page that starts with a few options. Based on what the user selects from dropdowns and by adding new fields, the form is created and at the end the form is submitted.
The problem is that, even if I can do validation checks on the server, and display a general validation message on the page. How should I pre-fill the form fields with the values that the user entered before pushing the submit button.
Three options:
normal POST -> the server render the form AS it was submitted (you should be able to rebuild the form server side)
ajax POST -> just redirect the user if validation succeeded
check before post -> synchronous ajax validation calls
There's a couple ways I could see handling this.
1) Do another AJAX call upon submission that does the validations and returns w/o a full post. It's not an elegant solution since it involves two round trips and depending on the validations would lead to performance issues.
2) When you return the json that is used for extending the form, have it instead turn 2 sets of json objects. The first set of objects would represent the data you're currently using. The second set in the array would represent the validation logic which you can then apply however you need upon the form's creation.
Neither way is truly elegant; however, the latter provides a way minimize round trips to do the validation.