-1

I am trying to make an AJAX call to ajax.htm file. using web-worker. If it works fine, the data must keep populating at the given interval.

I dont get an error, and the get message seems to be working, but the data is not being populated on page. The message posted by the worker.js is not being listened to in the main page. Here is the code.

My webWorker.html file is as follows:

    <!DOCTYPE html>
<html>
<head>
    <title>Worker Sample</title>
    <script type="text/javascript">
        var worker = new Worker('worker.js');
       // console.log(worker);
        document.addEventListener("DOMContentLoaded",function(){

            var result = document.querySelector('#result');
          //  console.log(result);


            worker.addEventListener('message', function (event) {
                console.log(event.data);
                createElement('li', result,'',JSON.parse(event.data));
            });
            worker.addEventListener('error', function(err){
                console.log("There was a problem",err);
            });
            worker.postMessage('start');
        });

        function createElement(elementType, parent, className, innerHTML, custom) {
                var element = document.createElement(elementType);
                if (parent) parent.appendChild(element);
                if (className) element.className = className;
                if (innerHTML) element.innerHTML = innerHTML;
                if (typeof custom !== 'undefined') {
                    for (var prop in custom) {
                        element.setAttribute(prop, custom[prop]);
                    }
                }
                return element;
            }

    </script>
</head>
<body>
<div>
    <ul id="result">Result</ul>
</div>

</body>
<html>

    my worker.js is as follows:
   /**
 * Created by lakshmi on 7/22/15.
 */


self.addEventListener('message', function (e) {

    if(e.data === 'start'){
        setInterval(function () {
            getData()
        }, 1000);
    }


    function getData() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET','ajaxcontent.htm');
            xhr.withCredentials = true;
           // xhr.setRequestHeader("Content-Type", "text/event-stream");
            xhr.addEventListener('readyStateChange',function(){
                if(xhr.readyState === 4){
                    self.postMessage(xhr.responseText);
                }
            });
            xhr.send();

    }

});

ajaxcontent.htm

content from external page
javascript novice
  • 777
  • 2
  • 9
  • 28

1 Answers1

-1

CORS error means, that you are doing request to other domain, and other domain did not allow cross domain request.Try to do request to your domain.Other domain can allow some headers or some request method types (POST,DELETE,GET,PUT), can allow access from all domains or from some domains.

If you are disagree with my answer, explain why.

Alex Nikulin
  • 8,194
  • 4
  • 35
  • 37
  • I guess my problem is more with the webworker part not working than the CORS error. I create a file for ajax content and tried printing out the file at constant intervals, but it doesn't seem to be working. Editing the question – javascript novice Jul 25 '15 at 21:47