简体   繁体   English

Xhr 多个请求 javascript

[英]Xhr multiple request javascript

I am using the following script to read the information from an html request.我正在使用以下脚本从 html 请求中读取信息。

function runSearch(searchid, fullMovie) {    
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        var fullMovie = JSON.parse(xhr.responseText);
        var movie = { title: fullMovie.Title, runtime: fullMovie.Runtime, plot: fullMovie.Plot };
        document.getElementById('Title').innerText = movie.title;
        document.getElementById('Runtime').innerText = movie.runtime;
        document.getElementById('Plot').innerText = movie.plot
    }
};
    xhr.open('GET', 'http://www.omdbapi.com/?i=' +searchid+ '&plot=short&r=json', true);
    xhr.send(null);
}

How can I do to change the searchid from the xhr.open to use the id="searchid" from a div tag?如何将 xhr.open 中的xhr.open为使用div标记中的id="searchid"

<div>
<div id="tt0110912">
    <h1 id="Title">Title from tt00110912</h1>
    <p id="Runtime">Runtime from id</p>
    <p id="Plot">Plot from id</p>
</div>
</div>
   <br>
<div>
<div id="tt3322364">
   <h1 id="Title">Title from tt3322364</h1>
   <p id="Runtime">Runtime from id</p>
   <p id="Plot">Plot from id`enter code here`</p>
</div>
</div>

Is it possible to run this script several times with different xhr requests?是否可以使用不同的 xhr 请求多次运行此脚本? How can i do this if possible?如果可能,我该怎么做?

EDIT: cant make the code work!编辑:不能使代码工作! Theoricaly i need the code to make an xhr request depending on the div id class and fill the information inside that div with the xhr response from that id.理论上,我需要代码根据 div id 类发出 xhr 请求,并使用来自该 id 的 xhr 响应填充该 div 内的信息。 Think of a movie database that will show specific movie information from a movie list.想象一个电影数据库,它将显示电影列表中的特定电影信息。

Wrap your code into function and then call it in loop or manually several times with different searchid values.将您的代码包装到函数中,然后使用不同的searchid值在循环中或手动多次调用它。

function runSearch(searchid, fullMovie) {

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == XMLHttpRequest.DONE) {
            var fullMovie = JSON.parse(xhr.responseText);
            var movie = { title: fullMovie.Title, runtime: fullMovie.Runtime, plot: fullMovie.Plot };
            document.getElementById('Title').innerText = movie.title;
            document.getElementById('Runtime').innerText = movie.runtime;
            document.getElementById('Plot').innerText = movie.plot
        }
    };

    xhr.open('GET', 'http://www.omdbapi.com/?i=' + searchid + '&plot=short&r=json', true);
    xhr.send(null);
}

Then call it like this:然后像这样调用它:

runSearch('your search id', fullMovie);

<div id="runSearch('tt0110912', fullMovie)">

JavaScript needs to go in a script element (or an intrinsic event attribute, but they are terrible and should be avoided). JavaScript 需要进入脚本元素(或内部事件属性,但它们很糟糕,应该避免)。

The id attribute is where you put an identifier for an element. id属性是放置元素标识符的地方。 It isn't JavaScript and it makes no sense to put JavaScript there.它不是 JavaScript,将 JavaScript 放在那里是没有意义的。

<script>
runSearch('tt0110912', fullMovie)
</script>

Your HTML is invalid.您的 HTML 无效。 An id must be unique in a document. id在文档中必须是唯一的。 You can't reuse the if Title (etc) within the same document.您不能在同一文档中重复使用 if Title (等)。 Look at using classes instead.看看使用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM