search

Home  >  Q&A  >  body text

node.js - vue-resource,如何改变响应格式?

使用vue-resource 发送一个get请求,响应数据是文本,却接收到的数据是blob类型。请问怎么破?

node.js 服务器代码:

router.get('/',function (req, res, next) {

var data = querystring.parse(url.parse(req.url).query);    
res.sendfile("./public/song/"+data.lyric,'utf8');

})

vue-resource 代码:

this.$http.get(url)

    .then(function (res) {
     console.log(res.body);
    })

响应内容:

PHP中文网PHP中文网2818 days ago868

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 16:21:49

    Sorry, I’m not very familiar with express and vue-resource

    Try to change the server code and specify the header

    router.get('/',function (req, res, next) {
        var data = querystring.parse(url.parse(req.url).query);    
        res.sendfile("./public/song/"+data.lyric,{
            headers:{
                'content-type':'text/plain'
            }
        });
    })

    If I remember correctly, the res of vue-resource has the text attribute

    this.$http.get(url)
        .then(function (res) {
            console.log(res.text);
        })

    It is recommended to use superagent or axios

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:21:49

    If I remember correctly, res.data is the response data

    reply
    0
  • Cancelreply