0

I have the following Javascript code to make a XMLHttpRequest to a server:

function createCORSRequest(method, url) {

  var xhr = new XMLHttpRequest();
  xhr.open(method, url, true);
  return xhr;
}


function makeCorsRequest(word) {

    var url = "https://localhost:8080/Method/Dictionary/" + word;

    var xhr = createCORSRequest('GET', url);
    xhr.onload = function() {
        var responseText = xhr.responseText;
        document.querySelector("#bar").innerHTML = responseText;
    };

    xhr.onerror = function() {
        document.querySelector("#bar").innerHTML = 'Connection not allowed';
    };

    xhr.send();
}

makeCorsRequest("word");

At the server, I've got a REST structure (written using Jersey) similiar to:

@Path("/Dictionary")
public class Main{

public Definition returnDefinition(String word){

    Definition definition = new Definition();
    try{

    ...//play with Definition object
    return definition;
    }


    catch(IOException IOE){
        ...
        return definition;
    }

}


@Path("{word}") 
@GET 
@Produces("text/xml ; charset=UTF-8")                   //"Definition" is already set as a XMLRoot element
public Definition main (@PathParam("word") String word){
    return returnDefinition(word);
}

}

I try to make this request in two environments:

First environment: The JS code is inside a normal web page. In this case, I receive the following error after trying to make the request:

XMLHttpRequest cannot load http://localhost:8080/Method/Dictionary/word. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Second environment: The JS code is inside a content script (that is itself inside a chrome extension). In this case, after about 30s of trying to make the request, I receive this:

Failed to load resource: net::ERR_TIMED_OUT

How to proceed?

EDIT: I've added a command to print something at the console in the beginning of the server method. And it is not printed. So, the requests are not reaching the server.

Mr Guliarte
  • 739
  • 1
  • 10
  • 27
  • Q: Does your browser support CORS (i.e. is it something *NEWER* than IE8)? If so, it sounds like server configuration. Please refer to this thread: http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – paulsm4 Oct 25 '15 at 21:56
  • Thanks to everyone who is trying to help! @paulsm4 yes, It's the newer version of Chrome! – Mr Guliarte Oct 25 '15 at 22:37
  • @wOxxOm When I said "Inside a normal page", I was trying to say that it is inside a normal HTML file, that I open with the browser...in the second case, this same code is inside a content-script : ) – Mr Guliarte Oct 25 '15 at 22:37
  • There is absolutely nothing wrong with your extension code - CORS is bypassed and the request is sent. Something is, however, wrong with your server. Check firewall settings, by the way. – Xan Oct 25 '15 at 23:11
  • @Xan Disabling the firewall showed no results. What could it be? I've not made much more at my Glassfish server than I showed here... – Mr Guliarte Oct 25 '15 at 23:27
  • Well, _something_'s going wrong on the server side. Check/make logs. See if connections actually reach and what's going wrong. – Xan Oct 25 '15 at 23:28
  • The server was reached at one of my tests, but not at the other ones...and there is nothing weird in the log files. Everything is strange here. – Mr Guliarte Oct 26 '15 at 00:05
  • Try [adding cors support on the server](http://stackoverflow.com/q/28065963/2587435) – Paul Samsotha Oct 26 '15 at 01:13
  • @peeskillet didn't work too : ( – Mr Guliarte Oct 26 '15 at 20:30

1 Answers1

0

Origin 'null' happens if you are running the page as a file in the browser, using the file:// protocol, rather than set it up in a local webserver and access it using http://.

vtortola
  • 34,709
  • 29
  • 161
  • 263