1

I am trying to get text from a service on the same server as my webserver. The link is something like this:

http://<OwnIPadres>:8080/calc/something?var=that

This is my code:

 function httpGet(theUrl)
  {
      alert(theUrl);
        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
            if (doc.readyState == XMLHttpRequest.DONE) {
                alert("text: " + doc.responseText );
                 document.getElementById('ctm').text = doc.responseText;
            }
        }
        doc.open("get", theUrl);
        doc.setRequestHeader("Content-Encoding", "UTF-8");
        doc.send();
  }

The url that i print in my first alert is the good one if i test in my browser, it is an html page with a table in it. But the alert of my text is empty? Is it a problem that the text is html?

9
  • document.getElementById('ctm').value = doc.responseText; Commented Nov 26, 2013 at 14:42
  • That just places the text in my html. But the alert already shows no text is returned... Commented Nov 26, 2013 at 14:44
  • Which alert ? alert("text: " + doc.responseText ); ? Commented Nov 26, 2013 at 14:45
  • Yes, alert("text: " + doc.responseText ); only gives "text: " back Commented Nov 26, 2013 at 14:46
  • That only means that your response doesn't have anything! Commented Nov 26, 2013 at 14:50

1 Answer 1

2

Actually, its quite ok that your 'text' is 'html'. The problem is that using a different port counts as cross-site scripting. Therefore, your XMLHttpRequest is being stopped by the browser before it actually reaches your page across port 8080.

I'm not sure what else you're doing before and around this code snippet, but you could try an iframe call to your url to get your data, or you could add an

Access-Control-Allow-Origin: http://:8080/

in your header (however that will only get you the most modern browsers).

Finally, you could pull in a JS framework like JQuery which could help you with pulling in this service data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.