1

the script below works when I run it through IE, but not when I run it through Chrome or Firefox. I get "Invocation errors occurred". Does anyone know why this is happening and what I need to do to fix it?

var isIE8 = window.XDomainRequest ? true : false;
var dictionary = createCrossDomainRequest();
var url = 'http://www.math.sjsu.edu/~foster/dictionary.txt';
makeRequest();

function createCrossDomainRequest() {
var request;
    if (isIE8) {
       request = new window.XDomainRequest();
  }
    else {
       request = new XMLHttpRequest();
  }
return request;
}

function makeRequest() {
if (dictionary) {
   if(isIE8) {
     dictionary.onload = requestSucceeded;
     dictionary.open("GET", 'http://www.math.sjsu.edu/~foster/dictionary.txt', true);
dictionary.send();
}
else {

dictionary.open("GET", 'http://www.math.sjsu.edu/~foster/dictionary.txt', true);
dictionary.withCredentials = true;
dictionary.onreadystatechange = handler;
dictionary.send();
 }
}
else {
   alert("No Invocation Took Place");
 }
}

function handler() {
 if (dictionary.readyState === 4){
   if (dictionary.status == 200){
     requestSucceeded();
  }
   else {
      alert("Invocation Errors Occurred");
   }
  }
 }

 function requestSucceeded() {
 resultText = dictionary.responseText;
 document.getElementById( 'demo' ).innerHTML += resultText;
 }
3
  • if you remove dictionary.withCredentials = true; does it make any difference? Commented Nov 11, 2015 at 15:51
  • It doesn't look like you're going to be able to do this with javascript. Thankfully, however, you can do this from the server side and then pass the result in to your javascript. Commented Nov 11, 2015 at 16:19
  • no, it does not matter if I use withCredentials or not Commented Nov 11, 2015 at 17:25

1 Answer 1

1

Are you sure IE8 isn't failing silently? It looks like handler() is being bypassed and requestSucceeded() called directly.

You need to have Access-Control-Allow-Origin headers set for the resource you're trying to access (dictionary.txt).

If you can't do that, then you could copy the file and host it on your domain.

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

3 Comments

I am using IE11 and it works. I tried putting the code in Firebug and I get an "undefined" message. It seems Firefox does not recognize the "GET" method.
how do i add Access-Control-Allow-Origin?
I also deleted the statement dictionary.onload = requestSucceeded; from the makeRequest function and moved the statements under the requestSucceeded function into the handler function. It still runs on IE11 but not on Firefox or Chrome.

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.