www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Can't connect to local server with XMLHttpRequest

  1. #1
    Join Date
    Sep 2013
    Posts
    2

    resolved [RESOLVED] Can't connect to local server with XMLHttpRequest

    Hi,

    I'm very new to Javascript, so if I come across as completely clueless, I apologize in advance...

    I want to create a demo in which I want to have a web page that connects to a server and then present the data from that server on the web page.

    I created a simple Java server that I have running locally (either in JBoss or Jetty) on port 8080.

    Now to read data from the server I use the following snippet:

    Code:
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET", "http://localhost:8080/simpleTest", false);
    try {
    	xmlhttp.send();
    } catch(err){
    	document.write(err.description);
    }
    xmlDoc=xmlhttp.responseXML;
    A few remarks:
    • The server works, when I put the URL in a web browser I get the result I expect
    • There's no problem with the XML. I've downloaded it and stored it in a local file. When I replace the URL in the xmlhttp.open command with the name of the local file, everything works fine
    • The error that is printed to the screen from the 'catch' is: undefined


    I have no idea what I'm doing wrong. If anybody can help me out or point out ways to investigate this further I would appreciate it very much.

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Try replacing this line:
    Code:
    document.write(err.description);
    with this;
    Code:
    console.log(err);
    Then check the javascript console and post the contents. That will help with figuring out the error

  3. #3
    Join Date
    Sep 2013
    Posts
    2
    I found the solution! I had to add the following header on the server side:
    Access-Control-Allow-Origin: *
    That did the trick

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles