0

We are trying to load a HREF target to a Div Tag using the Struts2-jQuery & Struts2 tags. Below is the code.

This is working fine as long as the s:url value is an internal URL, but if i change it to an external URL like www.google.com, nothing is happening when we click the link.

Is there anything different we have to do if the URL is an external URL ? i.e. URL outside the current application

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<html>
  <head>
    <sj:head/>
  </head>
  <body>
    <div id="div1">Div 1</div>
    <s:url var="ajaxTest" value="/AjaxTest.action"/>

    <sj:a id="link1" href="%{ajaxTest}" targets="div1">
      Internal Content
    </sj:a>

   <s:url var="ajaxXternal" value="www.google.com"/>

    <sj:a id="link2" href="%{ajaxXternal}" targets="div1">
      External Content
    </sj:a>

  </body>
</html>
yathirigan
  • 5,619
  • 22
  • 66
  • 104
  • 10/14 questions are answered; if the answers aren't to your satisfaction, don't accept them--but if they are, do. Only you can decide that. – Dave Newton Aug 22 '12 at 19:34

1 Answers1

3

Calling an external site with Ajax will create a cross-domain XHR request, which won't work.*

If you want to load from another domain, it should either support JSONP, or you can make a request from your app (an S2 action), which makes a normal request (non-Ajax) to the site via something like HttpClient. Stream that response back from your action, and the plugin will load the div with it.

* Unless the site's Access-Control-Allow-Origin gives you access, obviously.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • just saw the same in http://api.jquery.com/load/ [Additional Notes] section. The place i was testing the URLs were of different domains,but our actual applications will have different context roots under the same domain http://dashboard.company.com [link from here to the next to Applicaton URLs] , http://dashboard.company.com/xyz and http://dashboard.company.com/abc. So this might work-out in our actual sceanrio. Thanks for your answer. – yathirigan Aug 22 '12 at 19:57