Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在js中不点击按钮获取数据,我的页面自动获取数据,第二次调用按钮_Javascript_Html_Api_Es6 Promise_Fetch Api - Fatal编程技术网

Javascript 在js中不点击按钮获取数据,我的页面自动获取数据,第二次调用按钮

Javascript 在js中不点击按钮获取数据,我的页面自动获取数据,第二次调用按钮,javascript,html,api,es6-promise,fetch-api,Javascript,Html,Api,Es6 Promise,Fetch Api,有人能帮我吗?我在这里使用fetch api,它链接到一个按钮,我在这里使用fetch api请求get,但问题是,如果不单击按钮,我的数据将从api中获取。 当我第一次单击按钮获取数据时,它工作得很好,但在重新加载之后,我的数据将自动获取,而无需单击按钮。这里有什么问题?如何解决 easyhttp.html 简易http 得到 邮递 放 删除 easyHttpWithFetch.js document.getElementById("btn1").addEventLis

有人能帮我吗?我在这里使用fetch api,它链接到一个按钮,我在这里使用fetch api请求get,但问题是,如果不单击按钮,我的数据将从api中获取。 当我第一次单击按钮获取数据时,它工作得很好,但在重新加载之后,我的数据将自动获取,而无需单击按钮。这里有什么问题?如何解决

easyhttp.html


简易http
得到
邮递
放
删除
easyHttpWithFetch.js

document.getElementById("btn1").addEventListener("click",get("https://jsonplaceholder.typicode.com/posts"));
function get(url){
    fetch(url)
        .then(response => response.json())
        .then((data) => {
            let str = "";
            data.forEach(element => {
                str += '<li><ol type="a">';
                for (const key in element) {
                    if (Object.hasOwnProperty.call(element, key)) {
                        const value = element[key];
                        str+= `<li>${value}</li>`;
                    }
                }
                str += '</ol></li>';
                let getRequestData = document.getElementById("getRequestData");
                getRequestData.innerHTML = str;
            });
    }).catch((err) => {
        console.log(err);
    });
}
document.getElementById(“btn1”).addEventListener(“单击”,获取https://jsonplaceholder.typicode.com/posts"));
函数get(url){
获取(url)
.then(response=>response.json())
。然后((数据)=>{
设str=“”;
data.forEach(元素=>{
str+='
  • '; for(常量键入元素){ if(Object.hasOwnProperty.call(element,key)){ 常量值=元素[键]; str+=`
  • ${value}
  • `; } } str+=''; 让getRequestData=document.getElementById(“getRequestData”); getRequestData.innerHTML=str; }); }).catch((错误)=>{ 控制台日志(err); }); }
    addEventListener()的第二个参数是我们希望在单击时调用的函数名。但是您当前正试图通过立即传递
    url
    参数来执行
    get()
    方法。 这就是为什么在将
    btn1
    附加到单击事件时首先调用
    get()

    要解决此问题,请尝试使用arrow函数

    document.getElementById(“btn1”).addEventListener(“单击”,()=>get(“https://jsonplaceholder.typicode.com/posts"));
    函数get(url){
    获取(url)
    .then(response=>response.json())
    。然后((数据)=>{
    设str=“”;
    data.forEach(元素=>{
    str+='
  • '; for(常量键入元素){ if(Object.hasOwnProperty.call(element,key)){ 常量值=元素[键]; str+=`
  • ${value}
  • `; } } str+=''; 让getRequestData=document.getElementById(“getRequestData”); getRequestData.innerHTML=str; }); }).catch((错误)=>{ 控制台日志(err); }); }
    
    简易http
    得到
    邮递
    放
    删除
    
    magive dupe……更改为
    const url=”https://jsonplaceholder.typicode.com/posts"; document.getElementById(“btn1”).addEventListener(“单击“,()=>{fetch(url)…});})