0

I'm trying to call a page outside of apex, but it isn't working. When I click a button in apex a dynamic action is triggered. It is a javascript block which has the following code:

var xmlhttp;
var name = document.getElementById('P1_X').value;
if (window.XMLHttpRequest){
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
}
else{
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200){

    alert(xmlhttp.responseText);
  }
}
xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlhttp.setRequestHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
xmlhttp.setRequestHeader("Access-Control-Max-Age", "3600");
xmlhttp.setRequestHeader("Access-Control-Allow-Headers", "x-requested-with");
xmlhttp.open("GET","http://92.109.55.51:7070/xander/rest/templates/" + name,true);

xmlhttp.send();

I get the following error without the setRequestHeader lines:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://92.109.55.51:7070/xander/rest/templates/Test1. This can be fixed by moving the resource to the same domain or enabling CORS.

I've looked around the internet and tried at things to my header(Source) as shown in the code, but I don't know where to put the header part. Currently I'm getting:
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

How do I get apex to get the information from the other page with a dynamic action?

Gert Kommer
  • 1,163
  • 2
  • 22
  • 49
  • You can not set CORS on the clientside....That is what the serverside needs to provide! – epascarello Jan 19 '15 at 16:21
  • @epascarello So the serverside has to add those options to the header? – Gert Kommer Jan 19 '15 at 16:42
  • That is how CORS works. They have to give you permission to read the data. – epascarello Jan 19 '15 at 16:43
  • The target server is a java application with rest services(Dynamic web project) on a tomcat server. Do I have to add the header options to the tomcatserver or to the java files? – Gert Kommer Jan 19 '15 at 17:34
  • The problem is solved. with your comments on the problem and a little help from [this post](http://stackoverflow.com/questions/16296145/set-header-in-tomcat/16307321) its working. Except for firefox... weird. but thx:) – Gert Kommer Jan 19 '15 at 18:00

0 Answers0