【问题标题】:Weird get request when trying to change HTML attribute尝试更改 HTML 属性时出现奇怪的获取请求
【发布时间】:2016-04-18 23:31:18
【问题描述】:

所以我有一个 html 文件,单击按钮后,它会在单独的文件中调用此函数:

function getRandomVideoLink(){
    //AJAX request to /random-video
    console.log("ajax request");
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            console.log("inside if");
            console.log(xhttp.responseText);
            document.getElementById("myVideo").src = xhttp.responseText;
        }
    };
    xhttp.open("GET", "/random-videolink", true);
    xhttp.send();
}

问题是由于某种原因,它没有更改元素的 src 属性,而是向服务器发出了一个奇怪的 get 请求,我收到一个错误 404(正常,因为在 app.use() 中处理的请求不是支持我指定显示该错误)。它应该只更改 iframe 的属性并显示另一个视频。

另一方面,如果我通过

调用它
function change() {
    document.getElementById('myVideo').src = "https://www.youtube.com/embed/BfOdWSiyWoc";
}

它工作正常。注意:xhttps.responseText 与该链接相同。

相关的 HTML:

<iframe id="myVideo" width="420" height="345"
   src="http://www.youtube.com/embed/XGSy3_Czz8k">
</iframe>

【问题讨论】:

    标签: javascript html ajax asynchronous


    【解决方案1】:

    我认为问题在于您在xhttp.responseText 中获得了引用的网址

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2015-12-08
      • 2016-09-06
      • 2021-09-25
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 2011-10-25
      • 2023-03-07
      相关资源
      最近更新 更多