Question

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>
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top