Sunday 21 May 2017

REST server responds with javascript browser error (HTML) instead of json

I'm using Node.js (on AWS Lambda for alexa skill) to request my web server for a JSON file. But my server responds with a 'Javascript' not supported error html.

This is my code:

function httpsGet(myData, callback) {


// Update these options with the details of the web service you would like to call
var options = {
    host: 'alexa0788.byethost24.com',
    port: 80,
    path: '/sample.php',
    method: 'GET',
};

var req = http.request(options, res => {
    res.setEncoding('utf8');
    var returnData = "";

    res.on('data', chunk => {
        returnData = returnData + chunk;
    });

    res.on('end', () => {

        console.log(returnData);
        var pop = JSON.parse(returnData).firstName + JSON.parse(returnData).lastName;

        callback(pop);  // this will execute whatever function the caller defined, with one argument

    });

});
req.end();

}

How can I make my server respond with the intended json and not force the client to support javascript? At the moment, I'm having a php file output the json. I tried calling a .json file too directly, instead of making a php file output json, but I see the same error.



via Krish

No comments:

Post a Comment