3

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?

aynber
  • 22,380
  • 8
  • 50
  • 63
Lèse majesté
  • 7,923
  • 2
  • 33
  • 44
  • Since your question is a couple month old, do you came up with a solution in the meantime? I'm currently having the exact same problem. – soerface Oct 28 '14 at 15:11
  • @swege Unfortunately, no. I found some potential solutions online (none were very simple), but I couldn't get any of them to work. So I never posted them as solutions. My temporary fix to continue development was to simply create a debug mode for my app, in which all REST requests were sent via GET. – Lèse majesté Oct 29 '14 at 03:14

1 Answers1

1

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();
        }
    });
};