会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 16楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Promise实现网络请求-异步请求-同步显示</title>
</head>
<body>
<script>
    //1.获取新闻信息 和评论  1.先获取新闻信息---2.获取后从里面获取标识  3.请求评论
    function getNews() {
        var promise=new Promise((resolve, reject) => {
            $.ajax({
                type:get,
                success:(res)=>{
                    resolve(res);
                },
                error:(err)=>{
                    reject(err);
                }
            })
        });
        return promise;
    }
    //调用接口 获取数据
    getNews()
    .then((res)=>{
        console.log(res);
    },(err)=>{
       console.log(err);
    })
</script>
<!--利用script标签实现跨域,需要后台配合-->
<script src="19-实现跨域.php?id=1002"></script>
</body>
</html>

<?php
    $id=isset($_GET['id'])?$_GET['id']:'1';
    $con=new PDO('mysql:host=localhost;dbname=beixidb;port=3306;charset=utf8','root','');
    $result=$con->query("select * from xinwen where id={$id}");
    $err='没有成功接收到数据';
    if($result){
      $res=$result->fetch(PDO::FETCH_ASSOC);
      echo json_encode($res);

    }else{
      echo json_encode($err);
    }

?>

blob.png

老师,我不会弄了

WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6第二部分 18楼

image.png

WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 19楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 20楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 21楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 22楼

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jquery-3.6.1.min.js"></script>
</head>

<body>
    <script>
        function ajax(url) {
            return new Promise((resolve, reject) => {
                $.getJSON(url, function (data) {
                    resolve(data);
                }, function (err) {
                    reject(err);
                });
            });
        }
        async function getinfo1() {
            var ids = await ajax("http://iwenwiki.com/api/generator/list.php");
            var names = await ajax("http://iwenwiki.com/api/generator/id.php?id=" + ids[0]);
            var infos = await ajax("http://iwenwiki.com/api/generator/name.php?name=" + names.name);
            console.log(infos);
        }
        getinfo1();
        async function getinfo2() {
            var ids1 = await ajax("http://iwenwiki.com/api/generator/list.php");
            var names1 = await ajax("http://iwenwiki.com/api/generator/id.php?id=" + ids1[0]);
            var infos1 = await ajax("http://iwenwiki.com/api/generator/name.php?name=" + names1.name);
            return infos1;
        }
        console.log(getinfo2());
       
       
    </script>
</body>

</html>

image.png

getinfo1函数正常输出,getinfo2函数输错错误,,为什么??

WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 23楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6第一部分 24楼

image.png

WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6第一部分 27楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6第一部分 28楼
WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 30楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637