如何使用 ajax 和本地 JSON 文件将数据 POST 到网页?

发布于 2025-01-11 04:37:38 字数 5226 浏览 0 评论 0原文

我正在尝试通过使用 ajax 在 html 网页的输入字段中输入信息以及已经具有现有信息的本地 JSON 文件来更新列表,所有文件都与 .html 文件位于同一目录中。

该对象有 3 个属性:1. 名称,2. 物种,3. 最喜欢的食物。

我输入的信息应显示在“现有宠物”标题中。

这是 html 文档:

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

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta name="description"
        content="Convex admin is super flexible, powerful, clean &amp; modern responsive bootstrap 4 admin template with unlimited possibilities.">
    <meta name="keywords"
        content="admin template, Convex admin template, dashboard template, flat admin template, responsive admin template, web app">
    <meta name="author" content="PIXINVENT">
    <title>AJAX & JSON</title>
    <link rel="shortcut icon" type="image/x-icon" href="/3025033.png">
    <link rel="shortcut icon" type="image/png" href="/3025033.png">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <link
        href="https://fonts.googleapis.com/css?family=Rubik:300,400,500,700,900%7CMontserrat:300,400,500,600,700,800,900"
        rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/feather/style.min.css">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/simple-line-icons/style.css">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="app-assets/css/app.css">
</head>

<body style="display:flex; flex-direction:row; justify-content:center;
min-height:100vh;">
    
    <header>
        <h1><br><br><a class="font-weight-bold">JSON and AJAX</a></h1>

        <h4><a class="font-weight-bold">Existing Pets</a></h4>
        <ul id="orders">
        </ul>
        <h4>
            <a class="font-weight-bold">Add Pets</a>
        </h4>
        <p>Name: <input type="text" id="name"></p>
        <p>Species: <input type="text" id="species"></p>
        <p>Fav food: <input type="text" id="favfood"></p>

        <button id="addpets">Add!</button>

        <script src="app-assets/vendors/js/core/jquery-3.3.1.min.js"></script>
        <script src="app-assets/vendors/js/core/popper.min.js"></script>
        <script src="app-assets/vendors/js/core/bootstrap.min.js"></script>
        <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>

        <script src="/main.js"></script>

</body>

</html>

这是 .js 文件:

$(function () {
    var $data = $('#pets');
    var $name = $('#name');
    var $species = $('#species');
    var $favfood = $('#favfood');
    $.ajax({
        type: 'GET',
        url: '/animals-1.json',
        success: function (data) {
            $.each(data, function (i, newOrder) {
                $data.append('<li><a class="font-weight-bold">Name: </a>' + newOrder.name + ', <a class="font-weight-bold">Species: </a>' + newOrder.species + ', <a class="font-weight-bold">Favorite Food: </a>' + newOrder.favfood + "</li>");
            });
        },
        error: function () {
            Swal.fire({
                title: 'Error!',
                text: 'Your request could not be processed',
                icon: 'error',
                confirmButtonText: 'Ok'
            })
        }
    });
    $("#addpets").click(function(){

        var newp = {
            name: $name.val(),
            species: $species.val(),
            $favfood: $favfood.val(),
        };
        $.ajax({
            type: 'POST',
            url: '/animals-1.json',
            data: newp,
            success: function(newpts){
                $data.append('<li><a class="font-weight-bold">Name: </a>' + newpts.name + ', <a class="font-weight-bold">Species: </a>' + newpts.species + ', <a class="font-weight-bold">Favorite Food: </a>' + newpts.favfood + "</li>");
            },
            error: function(){
                Swal.fire({
                    title: 'Error!',
                    text: 'Your request could not be processed',
                    icon: 'error',
                    confirmButtonText: 'Ok'
                })  
            }
        });
    });
});

moveover,JSON 文件中的数据:

[
  {
    "name": "Meowsy",
    "species" : "cat",
    "favfood": "tuna"
  },
  {
    "name": "Barky",
    "species" : "dog",
    "favfood": "carrots"
  },
  {
    "name": "Purrpaws",
    "species" : "cat",
    "favfood": "mice"
  }
]

我已经仔细检查了 JSON 文件的名称目录等。

I'm trying to update a list by entering information into the input fields of the html webpage using ajax alongside a local JSON file which already has existing information, all the files are in the same directory as the .html file.

The object has 3 attributes: 1. Name, 2. Species, 3. Favorite Food.

The information I enter should display in the "Existing Pets" heading.

this is the html doc:

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

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <meta name="description"
        content="Convex admin is super flexible, powerful, clean & modern responsive bootstrap 4 admin template with unlimited possibilities.">
    <meta name="keywords"
        content="admin template, Convex admin template, dashboard template, flat admin template, responsive admin template, web app">
    <meta name="author" content="PIXINVENT">
    <title>AJAX & JSON</title>
    <link rel="shortcut icon" type="image/x-icon" href="/3025033.png">
    <link rel="shortcut icon" type="image/png" href="/3025033.png">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <link
        href="https://fonts.googleapis.com/css?family=Rubik:300,400,500,700,900%7CMontserrat:300,400,500,600,700,800,900"
        rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/feather/style.min.css">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/simple-line-icons/style.css">
    <link rel="stylesheet" type="text/css" href="app-assets/fonts/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="app-assets/css/app.css">
</head>

<body style="display:flex; flex-direction:row; justify-content:center;
min-height:100vh;">
    
    <header>
        <h1><br><br><a class="font-weight-bold">JSON and AJAX</a></h1>

        <h4><a class="font-weight-bold">Existing Pets</a></h4>
        <ul id="orders">
        </ul>
        <h4>
            <a class="font-weight-bold">Add Pets</a>
        </h4>
        <p>Name: <input type="text" id="name"></p>
        <p>Species: <input type="text" id="species"></p>
        <p>Fav food: <input type="text" id="favfood"></p>

        <button id="addpets">Add!</button>

        <script src="app-assets/vendors/js/core/jquery-3.3.1.min.js"></script>
        <script src="app-assets/vendors/js/core/popper.min.js"></script>
        <script src="app-assets/vendors/js/core/bootstrap.min.js"></script>
        <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>

        <script src="/main.js"></script>

</body>

</html>

This is the .js file:

$(function () {
    var $data = $('#pets');
    var $name = $('#name');
    var $species = $('#species');
    var $favfood = $('#favfood');
    $.ajax({
        type: 'GET',
        url: '/animals-1.json',
        success: function (data) {
            $.each(data, function (i, newOrder) {
                $data.append('<li><a class="font-weight-bold">Name: </a>' + newOrder.name + ', <a class="font-weight-bold">Species: </a>' + newOrder.species + ', <a class="font-weight-bold">Favorite Food: </a>' + newOrder.favfood + "</li>");
            });
        },
        error: function () {
            Swal.fire({
                title: 'Error!',
                text: 'Your request could not be processed',
                icon: 'error',
                confirmButtonText: 'Ok'
            })
        }
    });
    $("#addpets").click(function(){

        var newp = {
            name: $name.val(),
            species: $species.val(),
            $favfood: $favfood.val(),
        };
        $.ajax({
            type: 'POST',
            url: '/animals-1.json',
            data: newp,
            success: function(newpts){
                $data.append('<li><a class="font-weight-bold">Name: </a>' + newpts.name + ', <a class="font-weight-bold">Species: </a>' + newpts.species + ', <a class="font-weight-bold">Favorite Food: </a>' + newpts.favfood + "</li>");
            },
            error: function(){
                Swal.fire({
                    title: 'Error!',
                    text: 'Your request could not be processed',
                    icon: 'error',
                    confirmButtonText: 'Ok'
                })  
            }
        });
    });
});

moveover, data in the JSON file:

[
  {
    "name": "Meowsy",
    "species" : "cat",
    "favfood": "tuna"
  },
  {
    "name": "Barky",
    "species" : "dog",
    "favfood": "carrots"
  },
  {
    "name": "Purrpaws",
    "species" : "cat",
    "favfood": "mice"
  }
]

I've double checked the name directory etc of the JSON file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文