简体   繁体   中英

why does it not return JSON to browser

I am trying to send JSON format to browser to show. But it seems like it is not going to broweser plus there is an error like req.write part. The access url is 127.0.0.1:3000/media=video i can access to server but it dose not return JSON to browser.

var http = require('http');
var recommandCat=['top5','newest','like','random','recommened'];
var url =require('url');


http.createServer(function (req, res) {
  var url_parts = url.parse(req.url, true);
  var query = url_parts.query;
  var returnJson="";
  console.log(query.media);  
   switch (query.media) 
   {
    case 'photo':
        returnJson=photoGenerator();
        break;
        case 'video':
        returnJson=videoGenerator();
        break;
        case 'audio':
        returnJson=audioGenerator();
   }


   console.log(returnJson);
   res.write(returnJson);   


       }).listen(3000, '127.0.0.1');


    function videoGenerator()
   {
    var jsonArray = new Array();
    for(i=0;i<10;i++)
   {
    var data = new Object();         
     var recoCat = recommandCat[Math.floor(Math.random()*4)];        
     data = {
                    tomEngine:{ 
                    mediaType:"video",
                        recommendset:[

                                                    {recommendCat:"+recoCat+",

                                                    recommendResult:[

                                                   {

                                                    mediaId:"",

                                                    mediaEntry:[{
                                                        user1:{
                                                                    name:"sooin",
                                                                    rating:"3",
                                                                    views:"2",
                                                                    like:"ture",
                                                                    comment:"good"                                  
                                                        },
                                                        user2:{
                                                                    name:"sara",
                                                                    rating:"1",
                                                                    views:"4",
                                                                    like:"ture",
                                                                    comment:"good!"                                 
                                                        }                                       
                                                        }], 
                                                    view:"4",
                                                    rating:"4",
                                                    like:"10",
                                                    attribute:{
                                                                    smallUrl:"www",
                                                                    largeUrl:"llll",
                                                                    title:"aaaa"
                                                    },
                                             }]                                                 
                                               }
                        ]   
                    }
        };  
        jsonArray.push(data);
}

return jsonArray;
}

Maybe my json format is wrong. Thank you.

You probably have to send correct Content-Type header to client. Also response.end() should be called in the end of each response .

var body = JSON.stringify(returnJson);

res.writeHead(200, {
  'Content-Length': body.length,
  'Content-Type': 'application/json'
});

res.write(body);
res.end();

Also if the body of your response is not chunked, you can use only method response.end() like

res.end(body);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM