node.js - vue-resource,如何改变响应格式?
PHP中文网
PHP中文网 2017-04-17 16:19:49
0
2
853

使用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中文网

认证0级讲师

reply all(2)
左手右手慢动作

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

洪涛

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template