为什么POST请求成为GET请求?

时间:2017-01-20 07:37:40

标签: java jquery ajax spring spring-mvc

当我使用jQuery的$.ajax()$.post()方法将表单信息发送到服务器时,'数据' string被添加到url的末尾。为什么POST请求成为GET请求?表格代码如下所示

<form role="form" class="form-horizontal">
    <div class="box-body">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">Name</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="name" name="name" required>
            </div>
        </div>
        <div class="form-group">
            <label for="hospital" class="col-sm-2 control-label">Hospital</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="hospital" name="hospital" required>
            </div>
        </div>
        <div class="form-group">
            <label for="url" class="col-sm-2 control-label">URL</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="url" name="url" required>
            </div>
        </div>
        <div class="form-group">
            <label for="version" class="col-sm-2 control-label">Version</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="version" name="version" required>
            </div>
        </div>
        <div class="form-group">
            <label for="description" class="col-sm-2 control-label">Description</label>
                <div class="col-sm-10">
                    <textarea class="form-control" id="description" name="description" rows="3" required></textarea>
                </div>
        </div>
    </div>
    <div class="box-footer text-center">
        <button type="reset" class="btn btn-default">Reset</button>
        <button type="submit" class="btn btn-primary" id="submitBtn">Submit</button>
    </div>
</form>

Ajax代码

$("#submitBtn").submit(function(event) {
    event.preventDefault();
    var info = {};
    info.name = $("#name").val();
    info.hospital = $("#hospital").val();
    info.url = $("#url").val();
    info.version = $("#version").val();
    info.description = $("#description").val();
    $.post("/nuts/add", JSON.stringify(info), function(data) {
            console.log(data);
        }, "json");
}

网址总是像这样

http://localhost:8080/nuts/add.html?name=1&hospital=1&url=1&version=1&description=1

我的后端框架是Spring MVC,控制器代码如下所示

@RestController
@RequestMapping(value = "/nuts/add", produces = {APPLICATION_JSON_VALUE})
public class AddNutsApi {

    private MongoBasicDao<Nuts> mongoBasicDao;

    @Autowired
    public void setMongoBasicDao(MongoBasicDao<Nuts> mongoBasicDao) {
        this.mongoBasicDao = mongoBasicDao;
    }

    @RequestMapping(value = "", produces = {APPLICATION_JSON_VALUE}, method = RequestMethod.POST)
    public ResponseEntity<Void> addNutsPost(@RequestBody Nuts nuts) throws NotFoundException {
        if (nuts.getName() != null && nuts.getHospital() != null && nuts.getUrl() != null && nuts.getVersion() != null && nuts.getDescription() != null) {
            try {
                Nuts _nuts = new Nuts();
                _nuts.setName(new String(nuts.getName().getBytes("ISO-8859-1"), "UTF-8"));
                _nuts.setHospital(new String(nuts.getHospital().getBytes("ISO-8859-1"), "UTF-8"));
                _nuts.setUrl(new String(nuts.getUrl().getBytes("ISO-8859-1"), "UTF-8"));
                _nuts.setVersion(new String(nuts.getVersion().getBytes("ISO-8859-1"), "UTF-8"));
                _nuts.setDescription(new String(nuts.getDescription().getBytes("ISO-8859-1"), "UTF-8"));
                _nuts.setCreationTime(new Date());
                Integer mark = mongoBasicDao.getCollectionMark(Constant.COLLECTION_NUTS);
                _nuts.setMark(mark);
                mongoBasicDao.addObject(_nuts, Constant.COLLECTION_NUTS);
                return new ResponseEntity<>(HttpStatus.OK);
            } catch (Exception e) {
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }
        } else {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }

    }
}

我添加了jackson的依赖项(jackson-databind),并在Spring MVC配置文件中设置<mvc:annotation-driven/>。顺便说一下,DispatcherServlet的url-pattern是/。 谁能告诉我我哪里弄错了?非常感谢!

1 个答案:

答案 0 :(得分:2)

当您致电JSON.stringify(info)时,您将获得一个JSON字符串,例如像这样的东西:

{ "name": "1", "hospital": "1", "url": "1", "version": "1", "description": "1" }

你肯定会获得这样的查询字符串:

?name=1&hospital=1&url=1&version=1&description=1

这应该是您的提示,即JavaScript代码对您看到的GET请求负责。

问题是你绑定了提交函数错误。 $("#submitBtn").submit(...)没有做任何事情,因为<button type="submit">不会触发任何submit事件。 <form>会这样做。

如果忽略JavaScript代码,点击Submit按钮会触发表单提交,因为<form>元素没有method="post"属性,表格将以GET提交。

解决方案:将submit(...)绑定到<form>