Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.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 在React中使用异步从API恢复数据时出现问题_Javascript_Reactjs_Api_Async Await - Fatal编程技术网

Javascript 在React中使用异步从API恢复数据时出现问题

Javascript 在React中使用异步从API恢复数据时出现问题,javascript,reactjs,api,async-await,Javascript,Reactjs,Api,Async Await,我正在尝试在react中运行api,并将响应作为变量返回。api返回给定网页的标题 详细信息 api使用以下URL{将网站URL附加到末尾} 手动提交此URL将在浏览器中显示“Antstand the Bambol laptop stand”。我正在尝试使用此api在我的应用程序中设置变量 我的当前代码 async function titleAPI(props) { const baseUrl = 'http://textance.herokuapp.com/title/'

我正在尝试在react中运行api,并将响应作为变量返回。api返回给定网页的标题

详细信息 api使用以下URL{将网站URL附加到末尾}

手动提交此URL将在浏览器中显示“Antstand the Bambol laptop stand”。我正在尝试使用此api在我的应用程序中设置变量

我的当前代码

 async function titleAPI(props) {
        const baseUrl = 'http://textance.herokuapp.com/title/'
        const response = await fetch(baseUrl + props)
        const data = await response
        console.log('FUNCTION OUTPUT: ',data)
        return data
      }
      titleAPI(myUrl).then(console.log)
在运行代码时,我意想不到的收获如下(我期待着:“Antstand the Bambol laptop stand”)


我认为Cors指的是一个跨原点错误

正文:(…)
类型:'cors'
表示由于cors,您得到了不透明的响应。将“访问控制允许源站”发送到您的源站(或*所有源站),或在前端配置代理以更改所有请求的
Origin
头,以便服务器无法将其作为COR接收。axios具有配置选项。还提供了在开发环境中配置代理的机制

试试这种方法,它会显示您期望的结果 “蚂蚁架竹子笔记本电脑架”展览

FUNCTION OUTPUT:  
Response {type: "cors", url: "http://textance.herokuapp.com/title/www.antstand.com/", redirected: false, status: 200, ok: true, …}
type: "cors"
url: "http://textance.herokuapp.com/title/www.antstand.com/"
redirected: false
status: 200
ok: true
statusText: "OK"
headers: Headers {}
body: (...)
bodyUsed: false
__proto__: Response
 const titleAPI=async props =>{
        const baseUrl = 'http://textance.herokuapp.com/title/'
        const url = baseUrl + props
        const response = await fetch(url,{
            method:'GET',
            mode:"cors",
            credentials:"same-origin",
            cache:"no-cache",
            headers:{
                "Content-Type":"application/json",
                "Access-Control-Allow-Origin":"*",
            },
            redirect:"follow",
            referrer:"no-referrer"
        })
        let data = await response.text()
        return data
    }    
titleAPI('www.antstand.com/').then(data=>console.log(data))