<FORM NAME="form1" METHOD="POST" action="some.jsp">
<INPUT TYPE="text" NAME="first" VALUE="First">
<INPUT TYPE="text" NAME="second" VALUE="Second">
</FORM>
When the form is submitted in the background a POST method is sent to that jsp page with the parameters.
What I am trying to do is that I have an ajax call to a local mediator jsp page which should then take those parameters and post to a page on another domain (this is for me to circumvent the cross-domain problem with ajax calls in IE8).
How would I do an explicit post? Something that takes a URL and the parameters?
If all you are having issue with is posting the form, it is as simple as
document.forms['form1'].submit()
EDIT: In that case, see Using java.net.URLConnection to fire and handle HTTP requests for how to make a POST or GET request. I would recommend using request.getParameterMap() and iterating over that, dropping those parameters into the new outbound request.
http://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/connector/Request.html
Related
I am very new to cs cart. I have read their documentation about calling Ajax with form micrformats. I am able to create a Ajax request. But I would like to call a controller inside my add-on controllers/frondend/ directory names as mycheckout.php. I am using the a hidden file dispatch with the valu mycheckout.mymode. Can any one help me. I actually want to perform some action in my controller. But their documentation did not help me. Thanks in advance.
You can do something like this (use extra "cm-ajax-force" or "cm-ajax-full-render" classes if needed):
<form class="cm-ajax" name="your_name" action="{""|fn_url}" method="post">
<input type="hidden" name="result_ids" value="div_id_to_be_updated" />
{include file="buttons/go.tpl" but_name="mycheckout.mymode" alt=__("Ajax button")}
</form>
Or you can use any button you want (even <input type="submit" name="mychechkout.mymode" value="Ajax!">)
Note that the Ajax request can be also done via simple link:
<a class="cm-ajax cm-post" href="{"mycheckout.mymode?param1=value1¶m2=value2"|fn_url}" data-ca-target-id="div_id_to_be_updated">Ajax!</a>
Is it possible to create Ajax-only form in Dojo? I need to make AJAX request on button click/ENTER, but not to refresh the whole page.
This is my simple dialog content:
<div id="myDialog">
<table>
<tr>
<td>Name</td>
<td><input name="name" type="text"/></td>
</tr>
<tr>
<td>Owner</td>
<td><input name="owner" type="text"/></td>
</tr>
</table>
<div class="buttons">
<button class='ok'>OK</button>
<button class='cancel'>Cancel</button>
</div>
</div>
I convert it to form:
form = new Form({action: okAction, method: ''}, dojo.byId('myDialog'))
The problem is, that after creating the Dojo form my actions on buttons are no longer called, instead clicking any of them or pressing ENTER causes page refresh.
Is it possible to bind AJAX request with form, instead of whole page submit? In JSF2 it is functioning so.
I'm not a fan of using forms in AJAX applications, but I'd like to use Dojo validation.
The action property means you submit the page, causing a complete page refresh (depending on the return value of your okAction). If you want to create a client-side form submit, you need to use the onSubmit event handler, for example:
form.onSubmit = function(event) {
// Do this stuff
};
Your event handler will probably contain an AJAX request, like the one #James Jithin describes in his answer.
You can read more about the event handlers at the API documentation.
You can GET or POST the form using dojo. See the form attribute set during the xhr call.
dojo.xhrPost
Here, you can pass the id of the form and get it submitted to the server. Then on the onLoad, you can decide what to do with the response.
When i click a radbutton then postback changing page url as /blabla.aspx?btnMsg_ClientState=&btnCarDetails=Sorgula&btnCarDetails_ClientState=&btnPrice_ClientState=&btnReject_ClientState=# ..
First pop-up work but then break.
There are 5 extra buttons and seems all in page url. Where is coming these query strings ?
Ie Developer tools says:
ScriptResource.axd...
I cant find solution, please help.
It appears that the form on your page is configured to use a GET request when submitted.
<form id="form1" runat="server" method="get">
In this case all parameters on the page are specified as part of the URL, which includes the input elements that are used for rendering the button, as well as the other input elements on your page.
You can change this behavior by setting the method attribute of the form to post, so that a POST request is utilized for submitting the form.
<form id="form1" runat="server" method="post">
In this case the parameters will be passed in the message body of the HTTP request.
I have view which takes care of all the Ajax submits from the client side. And to differentiate them by I uses different submit button names such as this one
<input type="submit" value="Send" name="send_message">
Suggested from this question.
The only problem is that from the view side it doesn't seems to carry the name to the server side so I cannot use the following if-statement
if 'send_message' in request.POST:
It works if I send it normally with page fresh. But I want to use it with Ajax.
I came up with a hack that you can add this name with jQuery. Simply by after serializing() your data you then concatenate the name attribute by data += "&send_message"
Then the if statement will work. But it doesn't seems so clean. So I wonder if there's a better way to handle this? Or should I make different views to handle the different Ajax calls I have?
You really should post each form to a different URL.
If not, you could add a hidden input with the name of the form as the value.
<input name="form_name" type="hidden" value="form_1" />
views.py:
form_name = request.POST['form_name']
I have an issue with my portlet and I don't know exactly how to solve it.
My portlet adds or retrieves info from liferay's DB by inserting a name in 2 text fields.
After pressing the submit button, I see the response from the server, a JSON response like this:
{"id":301,"name":"Pepo"}
If a user correctly inserted or if the search throws a good result. I have to go back in the browser to see the portal again.
How can I use AJAX to pass the following URL dynamically from the portlet to the server without refreshing the page afterwards?
http://localhost:8080/c/portal/json_service?serviceClassName=com.liferay.test.service.TrabajadorServiceUtil&serviceMethodName=findByName&servletContextName=TrabajadorPlugin-portlet&serviceParameters=[param1]¶m1=NameInsertedByUser
Now I'm using the <form> tag like this:
<%
//Shows "New Employee" in the text field when portlet is rendered, or gets the user input and pass it as a param to the URL
PortletPreferences prefs = renderRequest.getPreferences();
String employee = (String)prefs.getValue("name", "New Employee");
%>
<form id="postForm" method="post" action="http://localhost:8080/c/portal/json_service">
<input name="serviceClassName" type="hidden" value="com.liferay.test.service.TrabajadorServiceUtil" />
<input name="serviceMethodName" type="hidden" value="create" />
<input name="servletContextName" type="hidden" value="TrabajadorPlugin-portlet" />
<input name="serviceParameters" type="hidden" value="[param]" />
<input name="param" type="text" value="<%=employee%>" />
<input type="submit" value="Submit"/>
</form>
I understand how AJAX works, but I need some help to create my function in order to achieve the URL to be correctly sent to the server for both GET and POST requests. This is my first try with AJAX.
Thank you very much, hope somebody understands my problem and could help me.
First of all, I see no point at all to use JSON services here. Just write ordinary portlet with MVC Controller, and in controller write action handling for corresponding actions (storing data, searching etc).
In controller you can directly call static methods like create or findByName from java class com.liferay.test.service.TrabajadorServiceUtil (or TrabajadorLocalServiceUtil) - that's how people usually do it.
If for some reason you really must use JSON, you should of course do these actions with AJAX calls - and render results using JavaScript.
Updating after question update:
The easiest and most correct way to send AJAX requests in Liferay would be to use AlloyUI JS framework that's a part of Liferay. You can read more on how to send AJAX requests with it here: http://www.liferay.com/web/nathan.cavanaugh/blog/-/blogs/4733559
In order to accomplish your goal I'd suggest implementing processAction(ActionRequest actRequest, ActionResponse actResponse) method in your controller/portlet.
In order to actually send data to it you'll have to have actionURL, which you can create using for example portlet:actionURL tag:
<portlet:actionURL /> or with Java code PortletURL actionUrl = portletResponse.createActionURL();
Then just submit your form using POST to this URL, and in actionRequest you'll have your parameters.