5
I am using an Elsevier API to fetch a book listing. To do this, I am using an example of them present at this link: http://apihtmlgen.elasticbeanstalk.com/sd_search.html I made my registration, I have my Apikey and I have the following code:
<html>
<head>
<title>Elsevier ScienceDirect Search via APIs</title>
<link rel="icon" type="image/png" href="images/favicon.ico" />
<script>
var xsl="";
var xml="";
function loadXMLDoc(dname) {
if (window.ActiveXObject) {
xhttp=new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
else {
//alert ("in ActiveXObject ELSE condition");
xhttp=new XMLHttpRequest();
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
function displayResult()
{
xml="";
var key="20d0c__________________________adb"; // API key value
document.getElementById("sd_results").innerHTML="";
//alert("in displayResult()");
var x = document.getElementById("form1");
var val = x.elements[0].value;
var apiReq="http://api.elsevier.com/content/search/index:SCIDIR?query="+val+"&apiKey="+key+"&xml-decode=true&httpAccept=application%2Fxml";
if (xsl == "") {
xsl=loadXMLDoc("sd_results_webkit.xsl");
}
//alert('getting xml');
xml=loadXMLDoc(apiReq);
// code for IE
if (window.ActiveXObject)
{
var ex='';
ex=xml.transformNode(xsl);
document.getElementById("sd_results").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("sd_results").appendChild(resultDocument);
}
}
function formReset() {
document.getElementById("form1").reset();
}
function displayPage(link) {
xml="";
document.getElementById("sd_results").innerHTML="";
var apiReq=link;
if (xsl == "") {
xsl=loadXMLDoc("sd_results_webkit.xsl");
}
//alert('getting xml');
xml=loadXMLDoc(apiReq);
// code for IE
if (window.ActiveXObject)
{
var ex='';
ex=xml.transformNode(xsl);
document.getElementById("sd_results").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("sd_results").appendChild(resultDocument);
}
}
</script>
<title>Elsevier ScienceDirect Search API</title>
</head>
<body onload="formReset();return false;">
<form id="form1" onkeypress="return event.keyCode != 13;">
<b>Please enter search keywords separated by space:</b> <input type="text" name="query" style="width:200px"/></form>
<input type="submit" onclick="displayResult();return false;" value="Search" />
<div id="sd_results"></div>
</body>
</html>
What happens is that when making the request gives me the following error "Request Cross-Origin blocked":
This error is about what? I am making this request on the link that has been logged, all right. What will be this mistake?
CORS fault. Tries to set in your server settings
Header always set Access-Control-Allow-Origin "*"
or by changing the asterisk to the URL you are trying to access. Send a Request Header of the kind Origin by Javascrip may also be necessary.– Bruno Augusto
@Brunoaugusto, I’ll look it up and I’ll give you the feedback. thanks
– pc_oc
@Brunoaugusto, this is put in the server settings? in my case I am using Cpanel. Or it is put in a file . htaccess?
– pc_oc
I know it’s CORS’s fault, but I’ve never experienced the problem. See if that article help you
– Bruno Augusto
@Brunoaugusto submit a request(GET) other than Origin? i just want to rescue the response from a php page by sending an id only that is another domain, to using XHR too, what to do?
– Paulo Roberto Rosa
Related question: How to make Ajax requests, with Jquery, in different domains?
– rray