如何从另一个网址获取数据?

时间:2018-06-26 23:11:11

标签: javascript jquery arrays ajax

这是一个难题。我正在对网页#1(http://www.blankmediagames.com/Trial/#start)上的这些不同报告进行投票,并且试图从网页#2(http://www.blankmediagames.com/Trial/viewReport.php?id=1388499)中获得一个名为“数据”的特定对象。但是,当我使用此代码时,它仅返回整个网页。

var tempVar;
var reportPage = new XMLHttpRequest();
reportPage.open('GET', '/Trial/viewReport.php?id=1388499', true);
reportPage.onreadystatechange = function() {
   if (reportPage.readyState === 4)  { 
   tempVar = (reportPage.responseText);
  }
};
reportPage.send(null);

我正在尝试从第2页上获取名为“数据”的对象,并将其存储在第1页上的另一个变量中。有什么办法可以做到吗?

这基本上是在第2页上找到的内容:

<!DOCTYPE html>
...
    <script>
            $(document).ready(function()
            {
                data = {"ReportID":"1388499","Username":"FatJellyBean","ReportFile":"Report1386875","Reason":"Gamethrowing","Appeal":"","Submitted":"Jun. 10, 2018 12:41 pm","numReports":2,"players":[{"username":"TooEzJukedBaited","ign":"ImJester","role":"SerialKiller","slot":1},{"username":"RISTR1K","ign":"Covfefe","role":"Doctor","slot":2},{"username":"balintka0607","ign":"William Phips","role":"Retributionist","slot":3},{"username":"JohnGotti","ign":"Karl Marx","role":"Veteran","slot":4},{"username":"Konrad4123","ign":"Pepe","role":"Escort","slot":5},{"username":"Celerian","ign":"Jeff","role":"Jester","slot":6},{"username":"foxesfan9","ign":"dabbingisnotcool","role":"Executioner","slot":7},{"username":"FatJellyBean","ign":"ifyoudonthitsubm","role":"Medium","slot":8},{"username":"GalaxyUnivern","ign":"Eliza Macbeth","role":"Godfather","slot":9},{"username":"Davve27","ign":"Katana","role":"Investigator","slot":10},{"username":"GayBobFaggyPants","ign":"Sarah Bishop","role":"Godfather","slot":11},{"username":"holysilman","ign":"Lil Tay","role":"Sheriff","slot":12},{"username":"pingvingen","ign":"Garfunkle","role":"Lookout","slot":13},{"username":"fefecute","ign":"Fefecute","role":"Framer","slot":14},{"username":"Frogers98","ign":"James Russel","role":"Jailor","slot":15}]};
                Trial.populateFilter(data);
                $
                                $('.reportedPlayer').html($('#FatJellyBean').next().html());
                $('#highlighter').show();
            });
            function highlight(elem,start,end)
            {
                var el=elem[0];
                var eln= (el.children.length > 0) ? el.lastChild : el.childNodes[0];
                var range=document.createRange();
                var sel=window.getSelection();
                start = (typeof start != 'undefined') ? start : 0;
                end = (typeof end != 'undefined') ? end : el.innerHTML.length;
                if(el.children.length > 0) start = end = 0;
                range.setStart(eln,start);
                range.setEnd(eln,end);
                sel.removeAllRanges();
                sel.addRange(range);
                el.focus();
            }
        </script>
...
</html>

2 个答案:

答案 0 :(得分:0)

可以将一个对象转移到另一页,但不能通过您尝试的方法。

有一些可以使用url参数的技巧:How to pass javascript variables from one html page to other html page

但是,更建议构建端点和API来传递信息。这种架构需要更多的工作,但可以解决您的确切问题:How to pass javascript object from one page to other

答案 1 :(得分:-1)

遗憾的是,Same Origin Policy阻止数据从其他域(包括子域)返回。不过,您可以尝试使用JSONP