问题描述
我试图让我的URL数组通过JQuery .get函数运行,以将网站的源代码转换为该函数外部的一个字符串。 我的代码如下。
var URL = ["http://website.org", "http://anothersite.com"];
var array = URL.map(function(fetch) {
var get = $.get(fetch, function(sourcecode) {
sourcecode = fetch;
}
我需要sourcecode变量是数组中所有URL上的源代码的组合。
1楼
taco
0
2015-08-07 06:13:38
您需要将一个变量放置在函数外部,例如下面的data
变量,然后使用+=
附加到该变量:
var URL = ["http://website.org", "http://anothersite.com"];
var array = URL.map(function(fetch) {
var data = null;
var get = $.get(fetch, function(sourcecode) {
data += fetch;
}
}
2楼
Rohan Kumar
0
2015-08-07 06:16:50
像这样尝试
var URL = ["http://website.org", "http://anothersite.com"];
var array = $(URL).map(function(fetch) {
var data='';
$.ajax({
url:fetch,
async:false,
success : function(d){
data=d;
}
});
return data;
}).get();
3楼
iplus26
0
2015-08-07 06:19:22
由于您使用的是jQuery,因此我想可能是遍历数组的更好方法。
var URL = ["http://website.org", "http://anothersite.com"]; var str = []; $.each(URL, function(index, fetch) { $.get(fetch, function(sourcecode) { str.push(sourcecode); // if you want an array }) }); str.join(''); // if you want a string console.log(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>