0

I am trying to post a comment to a nodejs server but i am getting 404 error. I have used cors module on server side to handles cors request. I am using typescript.

cors module: https://github.com/troygoode/node-cors

var app = express();
app.use(cors());
(<any>app).options('*', cors()); // include before other routes
app.use((<any>app).router);

Following is my endpoint code

app.post("/api/object/addcomment/:id", authenticatebearer(), (req, res) => {
    var id = this.ParseInt(req.params.id);
    var comment: string = req.body.CommentText;
    if (this.IsNullOrUndefined(id) && this.IsNullOrUndefined(comment))
        return this.SendInputError("input is empty.", res);

    (new OM.ObjectManager()).AddComment(req.user.userId, id, comment, (err, commentId) => {
        if (err) return this.SendInternalError(err.message, res);
        return res.json(commentId);
    });
});

I am calling above endpoint from angularjs app,

myApp.config(function ($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
});

function addComment(id, comment, callback) {
    var URL = Client.Config.RestUrl + "api/object/addcomment/" + id.toString() + '?access_token=' + this.RootScope.LogInUser.accessToken;
    this.$http.post(URL, JSON.stringify({ CommentText: comment }))
        .success((commentId, status) => {
            callback(null, commentId);
        })
        .error((data, status) => {
            callback(new Error("An unexpected error occured while adding comment."), null);
        });
}

This code is working properly in the desktop environment. Please see the network log from chrome, enter image description here

In phonegap, the function call executes successfully and the comment is added to the system but the $http.post is giving 404 error (gets response in error callback). Please see the network log from adb debugging tool (this works with android 4.4.2), enter image description here

I am struggling for this from last two day. Unable to find a solution. Please help me. Thanks in advance. Sorry for my english.

Pravin Patil
  • 418
  • 5
  • 23
  • not sure if it applies here but did you try to allow access to your server by configuring access origin in config.xml? – QuickFix Feb 02 '14 at 23:13
  • Thanks for the reply. There is no problem in the above code. I am using ionic framework for mobile. I used tab view in the above page and added form control as a child to the content directive provided by ionic framework. This was giving a problem. When I moved the form control the problem gets solved. – Pravin Patil Feb 03 '14 at 06:53
  • I think I had the same problem and figured it out. Check out http://stackoverflow.com/a/21839620/356016. – nshew13 Feb 17 '14 at 21:34

0 Answers0