【问题标题】:VueJs Web Applications Unable To Display ProductVueJs Web 应用程序无法显示产品
【发布时间】:2019-07-23 02:46:11
【问题描述】:

当用户单击购买按钮时,我想将产品添加到购物车页面。当我单击购买按钮并尝试显示购物车页面时,我在 google chrome 控制台窗口中收到错误 status:false

这里是 index.html 代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="bootstap.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js">

    </script>
    </head>
    <body>
        <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
            <ul class="navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link" href="index.html">Shop</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="index.html">Show All Products</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/AddProducts.html">Add Product</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="cart.html"> cart</a>
                </li>
                <li class="nav-item">
                    <label v-for="v in user" id="username" type="username:" {{v.username}} v-model="username"></label>
                </li>
            </ul>
        </nav>
        <br />
        <div class="container" id="app">
            <select class="form-control" id="sel1" v-on:change="applyfilters($event.target.value)">
                <option value="">Select Any value</option>

                <option v-for="v in vendors" :value="v.id">{{ v.name }} </option>
                <!-- <option value="v.id">{{v.name}}</option>-->
                <!--<option value="2">MI</option>-->
            </select>
            <br />
            <div class="row col-12" id="product-list">
                <div class="col-4 card mx-2 p-2" v-for="product in products" style="margin-bottom: 20px">
                    <b>Product Name :</b>{{product.name}}
                    <div class="row">
                        <div class="col-4 m-3 p-3">
                            <b>Price :</b>  {{product.price}}
                        </div>
                        <div class="col-4 m-3 p-3">
                            <b>Vendor :</b>  {{product.vendor.name}}
                        </div>
                        <div class="col-6 m-2 p-3">
                            <button class="col btn btn-primary" v-on:click="addToCart(product.id)">Buy</button>
                        </div>
                    </div>
                </div>
                <br />
            </div>
        </div>
    </body>
</html>
<script>
    let app = new Vue({
        el: "#app",
        data: {
            newTask: '',
            id: 0,
            url: '/todos',
            status: false,
            products: [],
            vendors: []
        },
        methods: {
            getAllProducts() {
                new Promise((resolve) => {
                    axios.get('/api/products').then(function (response) {
                        resolve(response.data)
                    })
                }).then((data) => {
                    this.products = data
                    // console.log(this.products)
                })
            },
            addToCart(id) {
                console.log(id)
                var obj = { productId: id };
                console.log(obj)
                new Promise((resolve) => {
                    axios.post('/api/cart/', obj).then(function (response) {
                        resolve(response.data)
                    })
                }).then((data) => {

                    console.log(data)
                    console.log(data.id)
                    if (!data.id) {

                        // console.log("fist login")
                      //  window.alert("Fist login ")
                       //  window.location = "signin.html";
                    }
                    else {
                        // console.log("successfully add to cart")
                        window.alert("product has been added to your cart")
                    }

                })
            },
            findAllVendors() {
                new Promise((resolve) => {
                    axios.get('/api/vendor').then(function (data) {
                        resolve(data.data)
                        // console.log(data.data)
                    })
                }).then((data) => {
                    this.vendors = data
                })
            },
            applyfilters(id) {
                new Promise((resolve) => {
                    axios.get('/api/products/' + id).then(function (response) {
                        resolve(response.data)
                    })
                }).then((data) => {
                    this.products = data
                    // console.log(this.products)
                })
            }
        }
    })
    app.getAllProducts();
    app.findAllVendors();</script>

这是 Cart.html 代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="bootstap.css">
    <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>-->
    <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>-->
    <!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>-->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js">

    </script>
</head>
<body>
    <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="index.html">Shop</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="index.html">Show All Products</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/AddProducts.html">Add Product</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="cart.html"> cart</a>
            </li>
        </ul>
    </nav>
    <br />
    <div class="container" id="app">
        <h2>Your cart</h2>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>quantity</th>
                    <th>Rate</th>
                    <th>vendor</th>
                    <th>Amount</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="cartItem in cartItems">
                    <td>{{cartItem.product.name}}</td>
                    <td>
                        <button type="button" class="btn btn-primary" v-on:click="changequantity(cartItem.productId,-1)">-</button>
                        {{cartItem.quantity}}
                        <button type="button" class="btn btn-primary" v-on:click="changequantity(cartItem.productId,+1)">+</button>
                    </td>
                    <td>{{cartItem.product.price}}</td>
                    <td>{{cartItem.product.vendor.name}}</td>
                    <td>{{cartItem.quantity*cartItem.product.price}}</td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td><b>Total</b></td>
                    <td>{{this.totalPrice}}</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>let app = new Vue({
        el: "#app",
    data: {

        totalPrice: 0,
       // price:0,

            cartItems:[]
        },
        methods:{
            fetAllcartItems(){
                new Promise((resolve)=>{
                    axios.get('/api/cart').then(function (response) {
                    resolve(response.data)
                })
                }).then((data)=>{
                        console.log(data)

                            this.cartItems=data
                           console.log(this.cartItems)
                    for (let  d in data){

                                this.totalPrice = this.totalPrice + (d.quantity) * (d.product.price);
                            }
                    // console.log(this.products)
                })
            },
            changequantity(id,quantity){
                var obj = {id : id , quantity: quantity}

                // let iddd = parseInt(id)
                console.log(this.cartItems)
                let index =this.cartItems.findIndex(item => item.productId == id)

                this.totalPrice = this.totalPrice + this.cartItems[index].product.price * quantity
                if(this.cartItems[index].quantity ===1 && quantity===-1){
                    this.cartItems.splice(index ,1);
                }
                new Promise((resolve)=>{
                    axios.post('/api/cart/add',obj).then(function (response) {
                    resolve(response.data)
                })
                }).then((data)=>{
                    console.log(data)

                    if(data.quantity>0)
                    this.cartItems[index].quantity = data.quantity
                    /*for(d of data){

                        this.totalPrice = this.totalPrice+ (d.quantity )* (d.product.price);
                    }*/
                })
                // location.reload();
            }
        }
    })
    app.fetAllcartItems();</script>
    </body>
</html>

这是我运行应用程序时的屏幕截图。

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    这是因为您试图从您的应用向同一个应用发出请求,浏览器会将其识别为跨域请求。

    如果您只是想快速修复它并按时完成,您可以安装一个允许跨源的 Chrome 扩展程序: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

    或者,如果您想以正确的方式进行操作,则需要拆分代码,让 API 侦听在与 FE 应用程序不同的端口上运行的方法。

    ;)

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2014-04-07
      相关资源
      最近更新 更多