0

I'm having some trouble within the console. I'm using the following code that takes an array of URL's and get the source code from those URL's.

var URL = ["http://www.website.com", "http://www.anotherwebsite.com"];
var str = [];

$.each(URL, function(index, fetch) {
   $.get(fetch, function(sourcecode) {
     str.push(sourcecode); 
   })
 });

console.log(str);

This returns the following array within the console:

enter image description here

However, I need that array to turn into a single string of all the source code. I tried using str.join but that doesn't work. I need the console to look like this:

enter image description here

Maybe im just missing a simple piece of code, but I cannot get it to work.

Note: This is within a Google Chrome Extension so cross domain requests are not an issue.

Dr. J
  • 1
  • 2
  • I tried using that - doesn't work. It returns an empty array. – Dr. J Aug 17 '15 at 17:52
  • Are you sure `console.log(str);` outputs array as in image. Because `$.get` is asynchronous that value may push at any time – Pranav C Balan Aug 17 '15 at 17:56
  • or `str = str + ` ... – Popnoodles Aug 17 '15 at 17:58
  • There was just an answer posted on that. I've tried it before, but returns an empty line within the console. Not sure why it does that especially since it is a normal array before you try to change it into an string. – Dr. J Aug 17 '15 at 18:08

1 Answers1

1

$.get is not what you are looking for. "Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol."

You want to use $.ajax instead.

This is going to get you close:

var URL = ["http://www.google.com", "http://www.jquery.com"];
var str = "";

$.each(URL, function(index, fetch) {
    $.ajax({
      url: fetch,
      dataType: "jsonp",
      success: function (sourcecode) {
         str += sourcecode;
      }
    });
 });

console.log(str);

http://jsfiddle.net/0zueyx97/

But you are still going to have some errors:

SyntaxError: expected expression, got '<'
SyntaxError: expected expression, got '<'

This is due to cross domain issues See Here for solutions

Community
  • 1
  • 1
Madness
  • 2,730
  • 3
  • 20
  • 29
  • Unfortunately, I've tried that. It returns a blank line. I'm completely clueless as to why it does that. – Dr. J Aug 17 '15 at 18:00
  • Update: It does work if the console.log is written next to str += sourcecode. However, the result in the console is the string of the source code from both URL's, but within different lines, almost like an array. I can access the variable outside of the function, and it is not a single string with the combination of the source code. – Dr. J Aug 17 '15 at 18:14
  • I tried it, and it returns a blank line within the console. I placed the console.log within the function and nothing shows in the console. This code is within a Google Chrome extension, do cross site requests are not an issue. – Dr. J Aug 17 '15 at 18:24
  • Still brings up a blank line, almost as if the array won't turn into a single string. Cross domain requests won't be an issue as this is within a Google Chrome Extension. – Dr. J Aug 17 '15 at 18:35