$ .post创建一个新表单

时间:2014-05-21 18:01:59

标签: jquery http-post

我希望用户能够创建新游戏。这是我到目前为止所拥有的。我遇到了$.post的问题。我如何使这项工作?

这是我的索引:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>

    <body>
        <form name="Name Of Game" action=$.post("data/games/create",  {title:""},function(data){});  method="get">
            New Game: <input type="text" name="game">
            <input type="submit" value="Create">
        </form>
    </body>
</html>

这是我的控制器:

using PlanningPoker.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PlanningPoker.Controllers
{
    public class GMController : ApiController
    {
        private static List<Game> games = new List<Game>() {
                new Game() {
                    ID = Guid.NewGuid(),
                    Title = "D&D"
                }
            };

        [Route("data/games")]
        public IEnumerable<Game> GetAllGames() {
            return games;
        }

        [Route("data/games/create")]
        public Guid CreateGame(string title) {
            Game g = new Game() {
                ID = Guid.NewGuid(),
                Title = title
            };

            games.Add(g);

            return g.ID;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该为表单的提交添加一个eventListener。

$('form').on('submit', function (){
      //TODO: put params in variable here

      $.post("data/games/create",  params,function(data){})
});