Getting grunt's connect server to accept POST requests on static files

291 Views Asked by At

I'm trying to use the connect middleware framework grunt comes preconfigured with to develop the front-end of my application, with static JSON files standing in for actual web services which I'll develop later.

However, sending a POST request to my static file results in a 404 error, even though a GET request with the same URL and parameters works just fine.

Can I configure grunt/connect to simply serve up my static file when a POST request is made to that URL?

1

There are 1 best solutions below

0
On

I did a trick in my source code to call GET method if the app uses Grunt Server:

var useGruntServer = window.location.href.indexOf("localhost:9000") >= 0;

self.jsonGet = function (url, dataIn, callback, errorCallBack) {
    $.ajax({
        data: dataIn,
        type: "GET",
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            if (callback) callback(result);
        },
        error: function () {
            if (errorCallBack) errorCallBack();
        }
    });
};

self.jsonPost = function (url, dataIn, callback, errorCallBack) {

    //Grunt Server Accepts only GET requests
    if (useGruntServer) {
        self.jsonGet(url, null, callback, errorCallBack);
        return;
    }

    $.ajax({
        data: self.objectToJsonString(dataIn),
        type: "POST",
        url: url,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            if (callback) callback(result);
        },
        error: function () {
            if (errorCallBack) errorCallBack();
        }
    });
};