AJAX call returns web page source - ajax

I'm making a GET request to a servlet through jQuery AJAX. In the servlet I'm using httpResponse.sendRedirect("abc.com"). In my AJAX call, the source of that webpage (abc.com) is being returned instead of the servlet redirecting me to that page.
Further info: I'm running all this on Websphere Portal Server 6.1 and am taking care of CORS using an IBM proxy servlet.
My question is this: how do I, from the servlet, redirect to the requested URI. Thanks!

Do redirection after ajax call completes(on success) using window.location.href . just return httpresponse in your function instead of redirection.

Related

Grails Spring Security only responds with ajax/JSON on login fail

I'm using spring security in a grails app and when a login fails it always redirects to "login/authfail?ajax=true" and a json is rendered.
I even tried to redefine the 'ajaxAuthFailUrl' to be the same as 'defaultFailureUrl', but even though it redirects to '/login/authfail?login_error=1' I still getting the same json.
I dig the spring plugin to see why it 'thinks' it's an ajax request. Then on 'SpringSecurityService.java' I put some debug messages on 'isAjax' method. The result was, first of all, it calls 'isAjax' 2 times in a row. And the request is considered an ajax for different reasons:
there is a header 'ajaxHeader'
there is an ajax parameter on the request
and finally when it checks the SavedRequest's headers, it also finds it to be an ajax call.
I confess I'm lost, I don't have a clue about how to try to fix it...
I'm using:
Grails 2.3.8
spring-security-core:2.0-RC4
runtime ':spring-security-acl:2.0-RC1'
and I have a custom authentication handler that is set on BootStrap:
authenticationProcessingFilter.authenticationSuccessHandler = new MyUrlAuthenticationSuccessHandler()

Load data from ajax request (json) on startup in AngularJs

I have this simple page that just needs to show contents that is loaded from an external url (ajax request, response in json format)
I should say I'm a AngularJS newbie.
I've googled a bunch and found different ways of doing this and couldn't manage to determine which is the correct/simple/up-to-date way to achieve this.
My 2 challenges -
Making the AJAX request run on startup (I can load the page before that happens and just load the contents one the ajax request finishes. Maybe show a 'Loading..' indicator)
Doing a ajax request correctly.
Here is my attempt. I know that the ajax request is never made because its not setup correctly.
You are getting into .error function:
http://jsbin.com/oDUsuVA/3/edit
For jsonp your response should be something like:
callback([
{
"title":"License Title 1",
"licenseUrl":"http://cnn.com",
"licenseText": " test"
}]);
Edit:
You can simply do .get() request too, but if you had to use jsonp request interface, you would have to correct response.
A Jsonp request always wraps the logic into a json callback wrapper function.
I just did $http.get instead of your $http.jsonp and it did work for me.

Java Servlet on Tomcat called from AJAX

I want to call a Servlet from ajax in a Java WebApp, using Tomcat7.
I used the annotation #WebServlet("/VServlet") to mapping it in the WebApp, because of Tomcat7 support Servlet 3.0.
I call the servlet with the jquery command "$.getJSON('VServlet', function(data){ ... });" but if I use the string 'VServlet', it doesn't work.
Only if I use the entire url 'http:// localhost:8080/WebAppName/VServlet' it works, but only in my pc. However I have to deploy it on a server that has a commercial domain name, obviously different from "localhost".
Please, can anyone tell me if there is a way to address the servlet with a relative url?
Because if I use a string like 'http://my.domain.it:8080/WebAppName/VServlet' it doesn't work on the Tomcat7 that is on the server machine.
Thanks.
------------------EDIT-----------------------
I try to explain better my problem.
I have a Java Servlet that return a json structure. The servlet is called in a javascript file that have to draw a graph in a web page, building a SVG image. I have to deploy this web app on a commercial Windows server with Tomcat7 installed.
The web app generally works. Any errors are displayed from the user point of view. Simply the image doesn't appear if I use that strings I wrote above, to call the servlet.
I suppose that the servlet doesn't respond because I use a wrong address/naming. If I use an absolute url, the servlet responds to the js caller, but I need a relative string, so if the domain name of the server will change, I won't change the code of the js file too.
When you call
$.getJSON('VServlet', ... );
you are making a HTTP GET request to an URL relative to the current URL. That is, if you're in /webapp/users/user.xhtml you'll be sending a request to /webapp/users/VServlet which obviously doesn't match the URL pattern of your servlet and not the thing you're trying to achieve.
You need to take into account the context path of a JSF application that's available in view as #{request.contextPath} to build up a proper absolute URL:
$.getJSON('#{request.contextPath}/VServlet', ... );
This will send a request to http://localhost:8080/webapp/VServlet.
In case you need to send request to a path relative to the root, such request URLs start with a slash, / and represent URLs relative to the root, no matter where you're right now:
$.getJSON('/VServlet', ... );
This will send a request to http://localhost:8080/VServlet.
if your js code is inside jsp page Instead of the code
$.getJSON('VServlet', function(data){ ... });
write
"$.getJSON('${request.contextPath}/VServlet', function(data){ ... });
other wise if this code is in some js file in a function something like
function someFunction(){
$.getJSON('VServlet', function(data){ ... });
}
and you are calling function someFunction() from jsp
then you add a parameter to the function like this -
function someFunction(url){
$.getJSON('VServlet', function(data){ ... });
}
and now call it from jsp like this -
someFunction('${request.contextPath}/VServlet')
because ${request.contextPath}/VServlet is an el tag and it will get compiled only in jsp page

Apache CXF Webservice failing on AJAX call

We have developed a webservice based on Apache CXF.
This is working fine when accessed normally that is using APIGEE or by using a JaxWsProxyFactoryBean (A clinet for Apache CXF). But when I tried to access this by providing the SOAP Address through AJAX call it is giving me the following exception:
INFO: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: No such operation: (HTTP GET PATH_INFO: /tata-ws-1.0/TataWeb)
at org.apache.cxf.interceptor.URIMappingInterceptor.handleMessage(URIMappingInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:236)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:89)
...
This is my AJAX call code happens on click of a button
<a onclick="sendRequest('GET','http://localhost:8080/tata-ws-1.0/services/TataWeb')"href="#">
Get Data:
function sendRequest(method, url)
{
method == 'POST';
{
http.open(method,url,true);
http.onreadystatechange = handleResponse;
http.send(null);
}
}
The URL for WSDL is correct because when i use
http://localhost:8080/tata-ws-1.0/services/TataWeb?wsdl it shows the WSDL for that webservice.
Please share your inputs.
Thanks.
Your need to send a SOAP request rather than a regular HTTP request.
Best way would be use something like this:
http://archive.plugins.jquery.com/project/jqSOAPClient

Extjs to call a RESTful webservice

I am trying to make a RESTful webservice call using Extjs. Below is the code i am using:
Ext.Ajax.request({ url: incomingURL ,
method: 'POST',
params: {param1:p1, param2:p2},
success: function(responseObject){
var obj = Ext.decode(responseObject.responseText);
alert(obj);
},
failure: function(responseObject){
var obj = Ext.decode(responseObject.responseText);
alert(obj);
}
});
but it does not work, the request is sent using OPTIONS method instead of POST.
I also tried to do the same thing using below code but result is the same:
var conn = new Ext.data.Connection();
conn.request({
url: incomingURL,
method: 'POST',
params: {param1:p1, param2:p2},
success: function(responseObject)
{
Ext.Msg.alert('Status', 'success');
},
failure: function(responseObject)
{
Ext.Msg.alert('Status', 'Failure');
}
});
But when i tried to do the same thing using basic ajax call ( using the browser objects directly i.e. XMLHttpRequest() or ActiveXObject("Microsoft.XMLHTTP")) it works fine and i get the response as expected.
Can anyone please help me, as i am not able to understand what i am doing wrong with extjs ajax call?
You can't make a standard AJAX call between domains. The URL for Ext.Ajax.request should be a relative one (relative to the script's origin).
If you want to do cross-domain calls, use a ScriptTagProxy or such.
The problem is exactly because of the reason ob1 and Chuck Hinson described.
I have an RESTful service, wich is running on Tomcat.
And i made a static client(no deployed to Tomcat) using ExtJs with Json reader.
I just made an html page with ExtJs integrated consuming REST service like url: http://localhost:8080/service/invoices/
And all the time ExtJs was making OPTIONS request, not GET or POST even if i was setting them as being used methods. The problem is this security feature, because Client is not the part of same application and i am doing AJAX call between domains.
As soon as i put my client to my Web application and deployed to Tomcat and started using relative calls it started working.
if you don't want cross-domain request, please remove the website prefix 'http://website' from propery url of ajax proxy.

Resources