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
Related
I am writing a REST server and client for it on Java. I do this for educational purpose.
My server is a web application that handle request from clients via servlet. After that it opens a storage conenction, retrieve data and send it as a json.
My client is a web aplication which has some simple web pages. User click a button, servlet on client handle and send(sic!) request to the server.
May be this way a little bit odd because on the client side moderbn world write just html pages with rich JS code, e.g. Bootstrap, Backbone Angular etc.and server side is wrote via JAX-RS or Spring, but my aim is to write this pet project on pure java as simple as it can be.
I faced with an issue that I don't understand how to send request from client side to the server side. I have received request from user in servlet and I want to send a reponse to the server.
What are possible ways to do that and what is the best one?
Thanks.
You can use Jquery Ajax to call your webservices with parameter required on server side. Update your view/jsp/html based on the data get from servlet.
Ajax Call from javascript :
function onButtonClick(){
$.ajax({
type: "post", //method type
dataType: "json", //response data type
url: ajaxUrl, //your webservice URL
data: "jsonobj", // Data to be send to server
success: function(response) // call back function after get successfull responce
{
// Process JSON response here
}
});
}
Your servlet code :
public class ResellerServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
//Process request here
// Convert your response in JSON and send it back to client
}
}
I am using REST API for searching. When an ajax call is fired, REST returns json from Java code
return JResponse.ok(searchResult).build() //searchResult is List of Custom object
In javascript I would stringfy that json and parse to show relevant data on screen.
var search = jQuery.parseJSON(JSON.stringify(data));
Now I want to secure/obfuscate json response returned from REST, so that anyone who directly hits APIs won't get readable response. I tried bson but bot able to implement it successfully. Didn't find much support on how to put collection object in bson and how to retrieve it back in JS while googling.
I will suggest you to go with tokens.
Every time when request made to server request must contain a token which change for every request as well check that the request header for ajax request. If it is an ajax request then and then only return result. Also add rule for no cross browser access.
I think if you did it your data will not be accessible to anyone by direct http request.
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.
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
I have a process that I need to follow, I hope this makes sense.
I have a JSP that builds up json data, and sends to a URL. This URL exists, and therefore will be successful.
However, a java based server socket class is listening on a port, and actually picks up the data being sent and processes it. It needs to generate a response for me to receive (ie success or failure codes of what it is going), that I am looking to pick up in the sucess function - but this java socket listener code does not intend on writing this to a JSP or something similar.
Any ideas how the java listener and my success function can meet so I can get the this response.
In my test, I was making the listener code place the response on a JSP and I pick that but, I want a way to not have to place onto a JSP. Is it a case the response (which will be a JSON data) HAS TO actually be served/held within a JSP/PHP/JSON file?
This is my send code below:
$.ajax({
type: "POST",
url: suppliedURL,
data: "jsonData=" + jsonString, // I have already done a json stringify on this.
success: function(data, textStatus, jqXHR) {
var jsonJqXHR = JSON.stringify(jqXHR);
alert('jsonJqXHR : ' + jsonJqXHR);
},
error:function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
},
dataType: "json"
});
Javascript running in a browser page has limited communication and interconnection capabilities. One of the things a javascript program running in virtually any browser can do is send out an HTTP request. So the obvious way to get data into a javascript program is via XMLHttpRequest, via the pattern some people call AJAX. This pattern is implemented in the jQuery ajax function.
The Javascript program needs to connect to an HTTP server - that is where the JSP comes in. It is a Java program that can respond to HTTP GET/POST etc. JSP or a similar HTTP-connected programming environment on the server, is necessary to serve data to the javascript program.
The only challenge therefore is moving the data from the Java socket program running on the server to the JSP also running on the server. One simple way to handle it is via a shared database or filesystem.