0

I try to send JSON data to client. I test typeof dataJ and it return object at console dataJ is printed dataJ=[object Object],[object Object]. At client, it displays nothing and it alert XMLHttpRequest.responseText is null and textStatus is error messages and errorThrown is null. Since it doesn't say error, I don't know what does I do wrong.

server site:

app.post('/myaction', async function (req, res) {
    async function next_func(req, res) {
        var myJson = await show();
        return myJson;
    }

    dataJ = await next_func(req, res);
    console.log("dataJ=" + dataJ);
    console.log(typeof dataJ)

    res.status(200);
    res.contentType('application/json');
    res.send(dataJ);
});

app.listen(8081, function () {
    console.log('Server running at http://127.0.0.1:8081/');
});

async function show() {
    var con = mysql.createConnection({
        host: "127.0.0.1",
        user: "root",
        password: "aaaaaaaa",
        database: "doto"
    });

    var sql = "select * from task_list";
    resultsArray = [];

    await new Promise((resolve, reject) => {
        con.connect((err, connection) => {
            if (err) return reject(err)

            con.query(sql, (err, rows, fields) => {
                if (err) return reject(err)

                resolve(rows.forEach((row) => {
                            resultsArray.push({
                                detail: row.details,
                                status: row.status,
                                subject: row.subject
                            });
                        })
                )
            })
        })
    })

    console.log("resultsArray" + resultsArray);
    return resultsArray;
}

client site:

$.fn.ajaxShow = function (st) {
    xhrct = $.ajax({
        type: 'POST',,
        data: {
            status: st
        },
        url: 'http://127.0.0.1:8081/myaction',
        success: function (data) {
            alert("function");

            $('#tb').empty();

            if (data != null) {
                var fotoData = $.parseJSON(data);

                $(fotoData).each(function (i, obx) {
                    alert("fotoData");

                    $('#tb').append('<tr>')
                        .append('<td>' + obx.detail + '</td>')
                        .append('<td>' + obx.status + '</td>')
                        .append('<td>' + obx.subject + '</td>')
                        .append('</tr>');
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("XMLHttpRequest: " + XMLHttpRequest.responseText);
            alert("textStatus: " + textStatus);
            alert("errorThrown: " + errorThrown);
        }
    });
}
Andreas
  • 21,535
  • 7
  • 47
  • 56
yaytip
  • 11
  • 2
  • Is this `express` or simple node `http` server? – Pavel Denisjuk Sep 01 '18 at 08:26
  • Either way, check this post on how to properly return data as JSON: https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express – Pavel Denisjuk Sep 01 '18 at 08:33
  • @ Pavel Denisjuk it's simple node http server – yaytip Sep 02 '18 at 06:58
  • I try res.setHeader('Content-Type', 'application/json');res.send(JSON.stringify(dataJ)); and console.log("send JSON : "+JSON.stringify(dataJ));display[{"detail":"detl0","status":"N","subject":"subj0"},{"detail":"detl1","status":"Y","subject":"subj1"}] but it still not work – yaytip Sep 02 '18 at 08:24

0 Answers0