客户端到Express服务器中的GET Request仅以text / html形式获得响应

我在快递服务器上有一个React应用程序。我正在使用fetch从客户端向服务器发出GET请求,当我在chrome上时响应作为Content-Type text / html返回,而在Postman中尝试时则返回为application / json。我想要将其作为JSON。

邮递员回应:

http://img3.mukewang.com/609c8e9e00010f6e03150110.jpg

从按钮调用时,Chrome中的“网络”标签:


http://img.mukewang.com/609c8ea90001e0d102790369.jpg

我尝试更改快递请求中的内容类型。


app.get('/spotify/search-track', (req,res) => {

    spotify.searchTrack(req.query.name).then(function(result)

    {

        res.setHeader("Content-Type", "application/json");

        res.send(result);

    });

});

响应GET请求:


fetch('/api/spotify/search-tracks?name=' + val, 

{

     method: "GET",

     data: null,

     headers : { "Accept": "application/json" }

}).then(response => response.json()).then(data => console.log(data));

我也在Chrome上收到此错误-可能是因为<来自HTML。

http://img3.mukewang.com/609c8eba00013bba05390034.jpg

冉冉说
浏览 243回答 2
2回答

MYYA

代替res.send(result)你可以使用res.status(200).json(result)

临摹微笑

我没有意识到请求正在调用/ api / spotify / search-tracks,而服务器正在期待/ search-track。我在那里不好;)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript