Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.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 如何使按钮调用使用fetch API的函数_Javascript_Fetch - Fatal编程技术网

Javascript 如何使按钮调用使用fetch API的函数

Javascript 如何使按钮调用使用fetch API的函数,javascript,fetch,Javascript,Fetch,我正在为一个正在进行的项目练习使用fetchapi,但在获取一个按钮来调用使用fetchapi访问外部API的函数时遇到了困难。继续获取“类型错误:无法获取”消息 consturi='1〕https://jsonplaceholder.typicode.com/posts'; 常量initDetails={ 方法:“get”, 标题:{ “内容类型”:“应用程序/json;字符集=utf-8” }, 模式:“cors” } 函数getData(){ 获取(uri,initDetails) 。然

我正在为一个正在进行的项目练习使用fetchapi,但在获取一个按钮来调用使用fetchapi访问外部API的函数时遇到了困难。继续获取“类型错误:无法获取”消息

consturi='1〕https://jsonplaceholder.typicode.com/posts';
常量initDetails={
方法:“get”,
标题:{
“内容类型”:“应用程序/json;字符集=utf-8”
},
模式:“cors”
}
函数getData(){
获取(uri,initDetails)
。然后(响应=>{
如果(response.status!==200){
console.log('看起来有问题。状态代码:'+
答复(现状);
返回;
}
log(response.headers.get(“内容类型”);
返回response.json();
}
)
。然后(myJson=>{
log(JSON.stringify(myJson));
})
.catch(错误=>{
log('Fetch Error:-S',err);
});
}
window.onload=function(){
让myButton=document.getElementById(“getData”);
myButton.addEventListener('单击',获取数据);
}

获取数据

试试这一款,它对我有用。

<html>

<head>
    <title>Page Title</title>
</head>

<body>
    <script>

        const uri = 'https://jsonplaceholder.typicode.com/posts';
        const initDetails = {
            method: 'get',
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            mode: "cors"
        }

        function GetData() {

            fetch( uri, initDetails )
                .then( response =>
                {
                    if ( response.status !== 200 )
                    {
                        console.log( 'Looks like there was a problem. Status Code: ' +
                            response.status );
                        return;
                    }

                    console.log( response.headers.get( "Content-Type" ) );
                    return response.json();
                }
                )
                .then( myJson =>
                {
                    console.log( JSON.stringify( myJson ) );
                } )
                .catch( err =>
                {
                    console.log( 'Fetch Error :-S', err );
                } );
        }
    </script>
        <button id='getData' onclick="GetData()">Get Data</button>

</body>

</html>

页面标题
常量uri=https://jsonplaceholder.typicode.com/posts';
常量initDetails={
方法:“get”,
标题:{
“内容类型”:“应用程序/json;字符集=utf-8”
},
模式:“cors”
}
函数GetData(){
获取(uri,initDetails)
。然后(响应=>
{
如果(response.status!==200)
{
console.log('看起来有问题。状态代码:'+
答复(现状);
返回;
}
log(response.headers.get(“内容类型”);
返回response.json();
}
)
。然后(myJson=>
{
log(JSON.stringify(myJson));
} )
.catch(错误=>
{
日志('Fetch Error:-S',err);
} );
}
获取数据

您的按钮位于表单中。表单中按钮的默认操作是提交表单,这将重新加载页面并中止获取请求

在表单提交处理程序中,将
type=“button”
添加到
元素中,或者(理想情况下,在本例中)完全删除