14 Replies - 836 Views - Last Post: 02 November 2015 - 02:32 PM

#1 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Can't get a response at CORS request

Posted 26 October 2015 - 02:54 PM

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 with the JS code 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


In my attempts to solve it, 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.

I

I've no idea of what to do. I've tried searching, asking in other forums, and nobody seems to know what is happening...please, help!

Is This A Good Question/Topic? 0
  • +

Replies To: Can't get a response at CORS request

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Can't get a response at CORS request

Posted 26 October 2015 - 02:59 PM

Edit: Ah, sorry for moving your post if it really belongs in Java

This post has been edited by CasiOo: 26 October 2015 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Can't get a response at CORS request

Posted 26 October 2015 - 03:03 PM

If it can't reach the server then that sounds like a network issue, maybe you're not using the correct URL. Either way, you can use your browser's developer console to actually look at the request to verify that it's being sent, you'll see that in the network tab.
Was This Post Helpful? 0
  • +
  • -

#4 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Re: Can't get a response at CORS request

Posted 26 October 2015 - 04:33 PM

Quote

Ah, sorry for moving your post if it really belongs in Java


I think it fits in both!

Quote

If it can't reach the server then that sounds like a network issue, maybe you're not using the correct URL. Either way, you can use your browser's developer console to actually look at the request to verify that it's being sent, you'll see that in the network tab.


Thank you, but I think the URL is correct! If I access it directly, I obtain the answer! And,yes, I think it is being sent...it really returns an error code after those 30 seconds.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Can't get a response at CORS request

Posted 27 October 2015 - 09:06 AM

If you're positive that the request is not reaching the server at all then it must be a network issue, I don't see another reason for the timeout. There could be issues on the server but if you're sure it's being sent and not reaching the server in the first place, then problems on the server wouldn't be the issue. If it's not reaching the server then that's a network issue.
Was This Post Helpful? 0
  • +
  • -

#6 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Re: Can't get a response at CORS request

Posted 28 October 2015 - 01:54 PM

Quote

If you're positive that the request is not reaching the server at all then it must be a network issue, I don't see another reason for the timeout. There could be issues on the server but if you're sure it's being sent and not reaching the server in the first place, then problems on the server wouldn't be the issue. If it's not reaching the server then that's a network issue.


But what kind of network issue? You know, I'm perfectly able to connect with my localhost. As I said, if I access the URL directly, I obtain the response. My guess is that Chrome is not directing my request, for some reason...
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Can't get a response at CORS request

Posted 28 October 2015 - 03:11 PM

As far as I know the browser uses the same pieces to send requests over ajax versus any other kind of request, I doubt that the browser would have completely separate modules for sending an ajax request versus a regular request. It's hard to guess what the problem might be if the only evidence of a problem is the timeout error message. Have you tried in other browsers? Have you checked the network tab to look at the request going out?
Was This Post Helpful? 0
  • +
  • -

#8 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Re: Can't get a response at CORS request

Posted 29 October 2015 - 04:20 PM

Quote

As far as I know the browser uses the same pieces to send requests over ajax versus any other kind of request, I doubt that the browser would have completely separate modules for sending an ajax request versus a regular request. It's hard to guess what the problem might be if the only evidence of a problem is the timeout error message. Have you tried in other browsers? Have you checked the network tab to look at the request going out?


Well, not tried in other browsers, since the app is for Chrome specifically. When spying the network tab, I can see the request was shoted, and that it's waiting for some response...after about 30s, the status of "pending" gets overwrited by a status of "failed".

And thanks for trying to help : )
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Can't get a response at CORS request

Posted 29 October 2015 - 04:34 PM

Have you requested the permissions for that?

https://developer.ch.../extensions/xhr
Was This Post Helpful? 0
  • +
  • -

#10 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Re: Can't get a response at CORS request

Posted 31 October 2015 - 06:00 AM

Quote

Have you requested the permissions for that?


In theory, yes:


 "permissions": [
        "tabs",
	"activeTab",
	"https://google.com.br/",
	"http://localhost,/*"
  ]

Was This Post Helpful? 0
  • +
  • -

#11 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Re: Can't get a response at CORS request

Posted 31 October 2015 - 07:59 AM

UPDATE: I'm guessing that the problem is httpS. I'm trying to make the request using https (because I'm obligued to by the browser), but if I try to accces it directly, I only succeed if I use http. Suggestions?
Was This Post Helpful? 0
  • +
  • -

#12 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Re: Can't get a response at CORS request

Posted 31 October 2015 - 08:07 AM

Confirming: If I access using http inside a website that also uses http, the request is successful. If the website uses https, the browser blocks my connection attemp,triggering the following error:

Mixed Content: The page at 'https://www.google.com.br/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://localhost:8080/Method/Dictionary/word'. This request has been blocked; the content must be served over HTTPS.
Was This Post Helpful? 0
  • +
  • -

#13 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Can't get a response at CORS request

Posted 02 November 2015 - 09:50 AM

OK, sounds like you need to use HTTPS then. Are you just using localhost for testing or is your extension actually going to rely on a local server running?
Was This Post Helpful? 0
  • +
  • -

#14 MrGuliarte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-October 15

Re: Can't get a response at CORS request

Posted 02 November 2015 - 12:47 PM

Quote

OK, sounds like you need to use HTTPS then. Are you just using localhost for testing or is your extension actually going to rely on a local server running?


Just for testing; the final API will be at a remote server. When searching, I found out that maybe I need to pay for a known institution to receive a signature that the browser trusts. Is this correct? Is there no other way (since that data I exchange with the server don't need further safety)?
Was This Post Helpful? 0
  • +
  • -

#15 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Can't get a response at CORS request

Posted 02 November 2015 - 02:32 PM

You can use a self-signed certificate but I don't know if Chrome will accept that for the extension.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1