【问题标题】:How can I bind an id to an ajax request url in vuejs?如何将 id 绑定到 vuejs 中的 ajax 请求 url?
【发布时间】:2018-04-18 09:58:18
【问题描述】:

我的页面网址为https://test.com/deaileditem.php?id=5

我需要获取这个 id 并在我的 ajax 请求末尾附加以获取 json 数据。

我是用代码拍的

var id = window.location.href.split('=').pop()
             console.log(id)

我的vue js代码是

<script>
still = new Vue({
    el: '#cat',
    data: {
        les: [],
    },


    mounted() {
        var m = this;
         var id = window.location.href.split('=').pop()
             console.log(id)
        $.ajax({
            url: "https://text.com/post/get/id",
            type: "GET",
            dataType: "JSON",

            success: function(e) {
                 m.les = e;
                console.log(e.les)

            },
        });
    },


})
</script>

而不是 url 中的“id”(url:“https://text.com/post/get/id”)。我需要附上我获得的 id,以便新的 url 是(url:“https://text.com/post/get/5”)。我怎样才能提供获得的 id 来获取所需的 json 数据。我在 js 方面非常基础。请帮帮我

【问题讨论】:

    标签: javascript jquery vue.js vue-component vue-router


    【解决方案1】:

    您还没有在url 中附加id

    改变

    url: "https://text.com/post/get/id",

    url: "https://text.com/post/get/" + id,

    【讨论】:

    • 它应该可以工作(更好 - 当然可能还有其他错误)
    • @med 如果有帮助,您可以将其标记为答案:)
    • 当然.. 我尝试标记.. 但它说.. 等待 2 分钟.. 非常感谢先生.. 为您的帮助.. 先生.. 我也可以问你一个疑问。 . 有没有其他方法可以将该 id 从上一页传递到本页?
    • @med 如果只想使用 javascript,您必须阅读 GET 参数并创建新的 url。否则,您也可以在服务器端处理它。您可以在 php 本身中获取 id,创建 url,并将其传递给前端(并在 ajax 中使用)
    • ohkkie..tanku 先生
    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 2012-08-26
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2019-04-21
    相关资源
    最近更新 更多