道歉询问看起来像是一个常见问题,但我似乎无法实现。

我想单击“ a”标签并打开一个新页面,根据MySQL数据库的ID显示文章。但是我有500。

谁能告诉我我的外套到底怎么了?谢谢。

这是'a'标签

 <article v-for='item in dataGroup'>
      <a :href="'http://localhost:8090/articlePage.html?articlePage?id='+item.ID" :name='pageId' target="__blank">
        <h4>{{item.title}}</h4>
        <p>{{item.intro}}</p>
      </a>
 </article>


我使用Vue-resource发送“获取”请求

const vm = new Vue({
    el: '#app',
    data: {
      dataGroup: [],
    },
    methods: {
      renderArticle(url) {
        this.$http.get(url, {

        }).then((res) => {
          this.dataGroup = res.data;
        }, (res) => {
          alert(res.status)
        })

      },
    },
    created() {
      this.renderArticle('/articlePage')

    }
  })


这是我的服务器代码

module.exports = () => {
var router = express.Router();
router.get('/', (req, res) => {
  db.query(`SELECT * FROM articles_table WHERE ID='${pageId.id}'`, (err, page) => {
    if (err) {
      console.error(err);
      res.status(500).send('database error').end();
    } else {
      res.send(page);
    }
  })
})

最佳答案

您尚未为articlePage定义服务器端路由
您实际上从未真正向服务器发送pageId,因此您不能在查询中使用它,因为服务器不知道该变量是什么,更不用说如何从中访问id了。
您没有定义catchall (*)路由来返回404错误代码,因此(大概)服务器正在响应500服务器错误,因为它不知道如何处理请求。


编辑

您的网址对我来说没有意义,应该是这样的:

https://localhost:8090/articlePage.html?articlePageId='+item.ID


然后在服务器端,您可以访问请求中查询字符串中的任何变量,如下所示:

req.query.articlePageId


req.query部分是魔术发生的地方

10-08 06:57
查看更多