【问题标题】:Why the POST request becomes a GET request?为什么 POST 请求变成了 GET 请求?
【发布时间】:2017-06-05 03:08:20
【问题描述】:

当我使用 jQuery 的 $.ajax()$.post() 方法将表单信息发送到服务器时,'data' 字符串会添加到 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配置文件中设置了&lt;mvc:annotation-driven/&gt;。顺便说一下,DispatcherServlet 的 url-pattern 是 / 。 谁能告诉我我哪里弄错了?非常感谢!

【问题讨论】:

  • 尝试在您的@RequestMapping 和html 表单method = "post" 上添加method = RequestMethod.POST
  • 错误是什么?检查网络选项卡上发送的请求/http 方法是什么,并且您有@RequestBody Nuts 您是否将其作为请求正文正确发送?在网络标签上分享信息
  • 尝试删除JSON.stringify函数
  • @drgPP 谢谢。 "post" 方法已经被 ajax 指定,method = RequestMethod.POST 也被方法 "addNutsPost" 指定

标签: java jquery ajax spring spring-mvc


【解决方案1】:

当你调用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(...) 不做任何事情,因为 &lt;button type="submit"&gt; 不会触发任何 submit 事件。 &lt;form&gt; 可以。

发生的情况是JavaScript代码被忽略,点击Submit按钮会触发表单的提交,由于&lt;form&gt;元素没有method="post"属性,表单将被提交作为GET

解决方案:将submit(...)绑定到&lt;form&gt;

【讨论】:

  • 感谢您非常清楚的解释。我已经将 sumbit(...) 绑定到表单元素,但现在我遇到了一个 415 http 错误,上面写着“不支持的媒体类型”:(
  • 因为您没有指定内容类型,这意味着它默认为application/x-www-form-urlencoded; charset=UTF-8,但您实际上发送的是application/json。使用$.ajax 而不是$.post,所以你可以设置contentType
猜你喜欢
  • 2014-07-24
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 2020-03-24
  • 2022-11-07
  • 2018-12-05
  • 2016-11-25
  • 2015-08-06
相关资源
最近更新 更多