Wednesday, 3 May 2017

.ajax call never returns

I'm using Express and Node. I've got some code that is posting JSON to another service that is adding it in a database.

The logic is doing what it is supposed to do, but the ajax call I am making is never returning, in the 'Network' tab in Chrome Dev tools it always shows as 'Pending' and eventually errors out with net::ERR_EMPTY_RESPONSE.

Can anyone tell me where I am going wrong?

Ajax Call

$.ajax
({
    type: "POST",
    url: "/order",
    contentType: 'application/json',
    data: orderDataJson,

    success: function () {

        alert("success!");
    },


    error: function (XMLHttpRequest, textStatus, errorThrown) {

        alert("Something went wrong checking out!\n" + textStatus + "\n" + errorThrown);
    }

});

This above routes to /order, which in turn posts the data to the other service:

app.post("/order", function(req, res, next)
{
    var options = {
        uri: endpoints.ordersUrl + "order",
        method: 'POST',
        json: true,
        body: req.body
    };

    request(options, function(error, response, body) {

        if (error !== null )
        {
            return;
        }

        if (response.statusCode == 200 && body != null && body != "")
        {

            if (body.error)
            {
                res.status(500);
                res.end();
                return;
            }

            res.end;
            return;

        }
        console.log(response.statusCode);

    });

});

This is the relevant code in the other service (it's correctly adding the content in the database)

if (request.method == 'POST')
{
    switch (path)
    {
        // Add a new order
        case "/order":

            var body = '';

            request.on('data', function (data) {
                body += data;
            });

            request.on('end', function () {

                var orderData = JSON.parse(body);

                // Insert into orders table
                var saleDate = getDate();

                var ordersQuery = "INSERT into orders (customerId, saledate)" +
                        " VALUES (" + orderData.customerId +",'" + saleDate + "')";

                db.query(ordersQuery, function(err, result)
                {
                    if (err)
                    {
                        throw err;
                    }

                    var orderId = result.insertId;

                    // Insert order details
                    for (var i=0; i < orderData.order.length; i++)
                    {
                        var productId = orderData.order[i].productId;
                        var quantity = orderData.order[i].quantity;

                        var orderDetailsQuery = "INSERT into orderdetails (orderID, productID, quantity)" +
                            "VALUES (" + orderId + "," + productId + "," + quantity +")";


                        db.query(orderDetailsQuery, function(err, result)
                        {
                            if (err)
                            {
                                throw err;
                            }
                        });

                    }
                });

                response.writeHead(200, {
                    'Access-Control-Allow-Origin': '*'
                });


            });

            break;



via Jbob

No comments:

Post a Comment