Form submission with POST method is truncated in Firefox - java

I have an Struts 2 application running in https.
I have a large form with data size approx 8KB, where the back end object is binding from the front end.
The form is submitted to the server properly in IE.
But when the form is submitted through Firefox then the data is not binding properly in the back end objects as well as the request method is truncated. we are submitting the form by using POST method but in server the method name is 'OST'.
Ex :
HttpServletRequest request;
request.getMethod() return "OST"
form is submitting with java script and jquery
Ex :
var data = $('#regHHFormID').serialize();
$.get('activateProfile.action', data, function(result) {
console.log(result);
}, "json");
Here is the sample data which is submitted to the server after serialize.
household.status=0&dualActiveDutyfamilyTypeID=15&household.key=897788905893&household.createFrom=CONSUMER&household.profileDatas%5B0%5D.type=SPONSOR&household.profileDatas%5B0%5D.profileID=1847&household.profileDatas%5B0%5D.profileNameData%5B0%5D.profileNameID=1885&household.profileDatas%5B0%5D.profileNameData%5B0%5D.firstName=sss&household.profileDatas%5B0%5D.profileNameData%5B0%5D.middleName=&household.profileDatas%5B0%5D.profileNameData%5B0%5D.lastName=sss&household.password=111111111aA&password2=111111111aA&household.familyGroupID=1&household.familyTypeID=4&household.profileDatas%5B0%5D.branchID=5&household.profileDatas%5B0%5D.command=&household.profileDatas%5B1%5D.type=SPOUSE&household.profileDatas%5B1%5D.profileID=&household.profileDatas%5B1%5D.profileNameData%5B0%5D.profileNameID=&household.profileDatas%5B1%5D.profileNameData%5B0%5D.firstName=ss&household.profileDatas%5B1%5D.profileNameData%5B0%5D.middleName=rrr&household.profileDatas%5B1%5D.profileNameData%5B0%5D.lastName=sss&household.profileDatas%5B1%5D.branchID=0&household.profileDatas%5B1%5D.command=&childProfile.profileNameData%5B0%5D.firstName=C4&childProfile.profileNameData%5B0%5D.middleName=CR&childProfile.profileNameData%5B0%5D.lastName=RG&childProfile.isUnBorn=false&birthDate=11.4.2013&childProfile.profileIATAnswerDatas%5B0%5D.questionID=1&childProfile.profileIATAnswerDatas%5B0%5D.profileIATAnswerID=&childProfile.profileIATAnswerDatas%5B0%5D.answer=false&childProfile.profileIATAnswerDatas%5B0%5D.answerDetails=Please+provide+information+that+will+help+us+support+your+child's+medical+needs.&childProfile.profileIATAnswerDatas%5B1%5D.questionID=2&childProfile.profileIATAnswerDatas%5B1%5D.profileIATAnswerID=&childProfile.profileIATAnswerDatas%5B1%5D.answer=false&childProfile.profileIATAnswerDatas%5B1%5D.answerDetails=Please+describe+accommodations+necessary+to+help+this+child+participate+in+Child+and+Youth+Programs.&household.profileDatas%5B2%5D.profileID=0&household.profileDatas%5B2%5D.type=CHILD&household.profileDatas%5B2%5D.profileNameData%5B0%5D.profileNameID=&household.profileDatas%5B2%5D.profileNameData%5B0%5D.firstName=C4&household.profileDatas%5B2%5D.profileNameData%5B0%5D.middleName=CR&household.profileDatas%5B2%5D.profileNameData%5B0%5D.lastName=RG&household.profileDatas%5B2%5D.birthDateConsumer=11.4.2013&household.profileDatas%5B2%5D.isUnBorn=false&household.profileDatas%5B2%5D.editStatus=&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B0%5D.questionID=1&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B0%5D.profileIATAnswerID=&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B0%5D.answer=false&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B1%5D.questionID=2&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B1%5D.profileIATAnswerID=&household.profileDatas%5B2%5D.profileIATAnswerDatas%5B1%5D.answer=false&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.profilePostalAddressID=&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.typeCode=PRIMARY&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.line1=D&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.line2=F&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.city=F&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.stateCode=AL&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.zip=12323&household.profileDatas%5B0%5D.profilePostalAddressDatas%5B0%5D.countryCode=USA&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.profilePhoneNumberID=0&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.phoneNumber=111.111.1111&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.typeCode=WORK&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.entityType=CONSUMER&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.entityID=0&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.belongsTo=1&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.editStatus=&household.profileDatas%5B0%5D.profilePhoneNumberDatas%5B0%5D.isPreferred=true&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.profileEmailAddressID=1232&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.emailAddress=ss%40fer.com&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.typeCode=PRIMARY&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.entityType=CONSUMER&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.entityID=1847&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.belongsTo=1&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.isNotify=true&household.profileDatas%5B0%5D.profileEmailAddressDatas%5B0%5D.editStatus=&household.emailNotificationDatas%5B0%5D.emailNotificationID=58&household.emailNotificationDatas%5B0%5D.isMandatory=false&household.emailNotificationDatas%5B0%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B0%5D.isSelected=true&household.emailNotificationDatas%5B1%5D.emailNotificationID=59&household.emailNotificationDatas%5B1%5D.isMandatory=true&household.emailNotificationDatas%5B2%5D.emailNotificationID=60&household.emailNotificationDatas%5B2%5D.isMandatory=false&household.emailNotificationDatas%5B2%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B2%5D.isSelected=true&household.emailNotificationDatas%5B3%5D.emailNotificationID=61&household.emailNotificationDatas%5B3%5D.isMandatory=false&household.emailNotificationDatas%5B3%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B3%5D.isSelected=true&household.emailNotificationDatas%5B4%5D.emailNotificationID=62&household.emailNotificationDatas%5B4%5D.isMandatory=false&household.emailNotificationDatas%5B4%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B4%5D.isSelected=true&household.emailNotificationDatas%5B5%5D.emailNotificationID=63&household.emailNotificationDatas%5B5%5D.isMandatory=true&household.emailNotificationDatas%5B6%5D.emailNotificationID=64&household.emailNotificationDatas%5B6%5D.isMandatory=false&household.emailNotificationDatas%5B6%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B6%5D.isSelected=true&household.emailNotificationDatas%5B7%5D.emailNotificationID=65&household.emailNotificationDatas%5B7%5D.isMandatory=false&household.emailNotificationDatas%5B7%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B7%5D.isSelected=true&household.emailNotificationDatas%5B8%5D.emailNotificationID=66&household.emailNotificationDatas%5B8%5D.isMandatory=false&household.emailNotificationDatas%5B8%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B8%5D.isSelected=true&household.emailNotificationDatas%5B9%5D.emailNotificationID=67&household.emailNotificationDatas%5B9%5D.isMandatory=false&household.emailNotificationDatas%5B9%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B9%5D.isSelected=true&household.emailNotificationDatas%5B10%5D.emailNotificationID=68&household.emailNotificationDatas%5B10%5D.isMandatory=true&household.emailNotificationDatas%5B11%5D.emailNotificationID=69&household.emailNotificationDatas%5B11%5D.isMandatory=false&household.emailNotificationDatas%5B11%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B11%5D.isSelected=true&household.emailNotificationDatas%5B12%5D.emailNotificationID=70&household.emailNotificationDatas%5B12%5D.isMandatory=false&household.emailNotificationDatas%5B12%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B12%5D.isSelected=true&household.emailNotificationDatas%5B13%5D.emailNotificationID=76&household.emailNotificationDatas%5B13%5D.isMandatory=false&household.emailNotificationDatas%5B13%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B13%5D.isSelected=true&household.emailNotificationDatas%5B14%5D.emailNotificationID=77&household.emailNotificationDatas%5B14%5D.isMandatory=false&household.emailNotificationDatas%5B14%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B14%5D.isSelected=true&household.emailNotificationDatas%5B15%5D.emailNotificationID=71&household.emailNotificationDatas%5B15%5D.isMandatory=false&household.emailNotificationDatas%5B15%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B15%5D.isSelected=true&household.emailNotificationDatas%5B16%5D.emailNotificationID=82&household.emailNotificationDatas%5B16%5D.isMandatory=false&household.emailNotificationDatas%5B16%5D.isSelected=true&__checkbox_household.emailNotificationDatas%5B16%5D.isSelected=true&household.emailNotificationDatas%5B17%5D.emailNotificationID=72&household.emailNotificationDatas%5B17%5D.isMandatory=true&household.emailNotificationDatas%5B18%5D.emailNotificationID=73&household.emailNotificationDatas%5B18%5D.isMandatory=true&household.emailNotificationDatas%5B19%5D.emailNotificationID=74&household.emailNotificationDatas%5B19%5D.isMandatory=true&household.emailNotificationDatas%5B20%5D.emailNotificationID=75&household.emailNotificationDatas%5B20%5D.isMandatory=true
I am not finding any clue why this is happening.

Jquery get method in turn calls the AJAX only. so you can think of using the AJAX api of Jquery
$.ajax({
url:"activateProfile.action"
type: "POST",
dataType : "json"
data: data,
success:function(data){
console.log('in your success step');
}
});
Make sure to declare the cache according to your usage. Hope this helps.

If your request.getMethod() returns "OST" this means that a method is not a valid HTTP one. And the browser revert to default which is "GET".
This method has bounds on data send in the URL. It could be limited by the length of the URL itself. Check the <form> tag or if you are using Ajax request the method "POST" is used to encapsulate the data with the request.

Related

jQuery manipulating java Hashmap

Can we use Jquery to manipulate Hashmap directly or i sud manipulate only the DOM data . I have a complex scenario where i need to change the java objects(basically HashMap ) from jquery get Request and get the response and print that in the jsp. User can send number of request and get seperate response from servlet based on request and display the data.
after every request i have to put the map in session and than in another request i get the same in session and do the updates and put it back in session.
JqueryGet(parameter) to Java.
Updating Hashmap according to parameter.
Putting hashmap in session . ( session.setAttribute("map",map))
sending response back to Jsp in jquery and print results in jsp.
Than another request send to Java .
it will get the map from session session.getAttribute("map",map) and than update the map based on new request .
Put the map again in session and so on....Than i have a submit button finally which will show the new data on the page and then update the server.
Is this the right approach? My functionality is working fine as of now in Dev environment. But i am worried whether i should use DOM. if i will use DOM it will be very complex since i have to manipulate much values of Hashmap based on request.
Here is the jquery code which is sending some string and array parameters:
$.ajax({
url: '<%=portalContext.createTemplateProcessURI()%>'
+'?s1='+ $("#networkBox1").val()+'&box1=Box1&tick=add&val1='+ allvs+'&s2='+ $("#networkBox2").val()+'&box2=Box2&val2='+ allvs,
type: 'get',
dataType: 'text',
async: false,
success: function(data) {
Processbox(data); // This function displaying the result.
}
});

Ajax post call to a rest service through servlet

I have a little problem:
I'm developing a jQuery Mobile app and i need to make an ajax post call to a rest service.
I create the pages dynamically using servlets.
I tried to make the ajax call to the rest url:
http://localhost:8181/myRestServicePath/func?key=value
from a page whit the following url:
http://localhost:8080/Mypage
but i get a cross-orign error from the browser.
So i'm tryng to perform this call, passing through a Java servlet using the doPost() method.
Now, my intent is to make an ajax post call from
http://localhost:8080/Mypage
to:
http://localhost:8080/myServletPath/func?key=value
and this servlet should redirect the POST request to my RestService:
http://localhost:8181/myRestServicePath/func?key=value
How can i perform this redirection?
There could be several ways to achieve that. I am just sharing 2 commons possibilities as below.
For POST requests:-
You will need to use Apache HTTPClient in your servlet to send the request to web services and get a response. After getting the response you can send that response to your page.
For GET Requests:-
You don't need extra servlet for cross domain request. You can use JSONP
jQuery example:
$.ajax({
url:"http://localhost:8080/myServletPath/func?key=value",
dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
success:function(json){
// do stuff with json (in this case an array)
alert("Success");
},
error:function(){
alert("Error");
},
});
For back end example visit Java J2EE Tutorial for Cross Domain JSONP

Is it possible to submit a form and send an ajax request at the same time jquery

I'd like to submit a form and, at the same time, send an ajax request to my Spring MVC Server. For it I have a form like this:
<form id='confirma' method='post' action='confirma'>
<button type='submit' id='save' class='btn btn-success' value='Save'>Enviar</button>
</form>
In my server, I would have two methods to receive both requests (the ajax request and the normal form submission).
// receive from form
#RequestMapping("fromForm")
public String fromForm(Model model)
{
// get atb1 values from database
model.addAttribute("atb1", atb1);
return "file.jsp";
}
// receive from ajax
#RequestMapping("fromAjax")
public void fromAjax(String jsonData)
{
// deserialize json Data and insert it on database
System.out.println("data received!");
}
My intention is to receive the data from the Ajax request, save it in database, and get this data in the form submission method, called, in this example, fromForm.
I know this might look crazy but I'm doing this because I have a sequence of submit forms and I'd like to make them independent from each other so I could call any of them without have passed the data from the previously form.
I have tried to deal with it by using jQuery. So this is what I've done:
function ajaxRequest() {
var jsonArray = new Array();
// Complete the jsonArray with an Array of objects created by myself
$.ajax({
url: "fromAjax",
cache: false,
contentType: false,
processData: false,
data: JSON.stringify(jsonArray, null, 4),
type: 'POST',
success: function (data, textStatus, jqXHR) {
console.log("success ajax");
return true;
},
error: function (xhr, status, error) {
console.log("error ajax");
return false;
}
});
}
$(document).on('submit', '#confirma', function(event) {
event.preventDefault();
// by return this method, I would just call the another jsp page, if the ajax
// request is completed successfully
return ajaxRequest();
});
This is not working though.. If I remove the event.preventDefault in the form submission, I only receive the form submission on the server. If I have the event.preventDefault call, only the ajax request hits the server.
How can I make both of the requests hit the server?
Think of this from the browser's perspective. Imagine that it has successfully fired both a XmlHttpRequest to /fromAjax and a regular form submission POST to /confirma. Now imagine that the server responds to the POST /confirma first. What is the browser to do when the form submission response contains a new HTML page to display, or is a redirect to another page - what should the browser do with the (still pending) XmlHttpRequest?
Or if you look at it from the server's perspective - what should happen when the server processes the request to your fromForm() method before it has received or begin to process the request to the fromAjax() method?
If your intention is to
receive the data from the Ajax request, save it in database, and get this data in the form submission method
then I would first send the ajax request and then when you receive the response, trigger the form submission from Javascript, so that these requests happen serially (one after the other) rather than in parallel.

Jquery $.get call to servlet not returning any values

I'm attempting to use jquery to send information to a servlet and receive a response from the servlet. I know my problem is not with the servlet because when I paste the url
http://localhost:8080/WebPaymentSolution?secureToken=abcd1234569780jfhgutinjhuyikfj746534&user=Conner
I get the proper response back in the browser. Here is the complete jquery code I am using.
$.get(
"http://localhost:8080/WebPaymentSolution",
{ secureToken : 'abcd1234569780jfhgutinjhuyikfj746534', user : 'Conner' },
function( data ) {
$( '#hss_iframe' ).html( data );
}
);
I am currently not getting a response back from the servlet.
I've actually solved this problem but it only works if you have control over the servlet(server side solution). I set the header response the servlet gives back to allow access control from any origin. It's working perfectly now. Here's the line of code I added to make it work.
response.setHeader("Access-Control-Allow-Origin", "*");
I know this doesn't answer the question of how you do this from jQuery but this is the best solution for me so I am going with it.
try it with:
$.ajax({
url: url,
data: data,
type: 'get'
success: function(data){alert(data)},
dataType: dataType
});
to enshure that you are usign get instead of post
and look with firebug if what happened with the request. if you get a "Permanently Moved" in firefox you are trying to call an ajax request to another domain from wherer you script has loaded
if it is so, google:
Cross-Site-Scripting => XSS

jQuery AJAX call to Java/Wicket server gets no response in IE (6/7/8)

Note: This relates to a previous question.
I have a Wicket page that has a form with some complex client-side interactions that I decided to use jQuery for instead of Wicket (long discussion, I know). Essentially I'm just building a JSON object, submitting it via AJAX, and performing some action on completion. The call works fine in Firefox, but not in any version of IE. I've already verified that there are no extraneous commas. Here's the code:
var lookup = {
'name': name,
'description': description,
'items': [{
'name': itemName,
'value': itemValue
}]
};
$.ajax({
type: 'post',
url: '${callbackURL}', // This file is parsed by Wicket and receives a dynamic callback URL here. This is not jQuery!
cache: false,
data: {'lookup': JSON.stringify(lookup)},
contentType: 'application/json',
complete: function() {
alert('This never gets called in IE!')
}
});
Any suggestions? Thanks!
Update: It appears my problem is due to IE caching the resources. I've put no-cache code in my HTML file, but it seems that either the page is still getting cached (and by extension, the JS it references), or the JS file with my jQuery code in it is being cached with the old callback URL in it so that the server doesn't respond because there's nothing at that URL anymore. When I remove the pretty URLs everything works fine (because every time Wicket generates the URL, it's unique). Still, shouldn't the complete function get called even if there's no server response?
This was due to aggressive IE caching. It was resolved by adding a uniquely generated parameter to the URL so the browser would think it's a new URL every time.

Categories

Resources