【问题标题】:jQuery ajax Get 404 Not FoundjQuery ajax 获取 404 未找到
【发布时间】:2017-11-03 22:25:15
【问题描述】:

我需要向服务器发送一个 http POST。我有一个像下面这样的 JS:

mm();
function mm() {
                $.ajax({
                    method : "POST",
                    contentType : "application/json",
                    url : "/address/sampleAddress",
                    dataType : "json",
                    timeout : 100000,
                    success : function(data) {
                        console.info("Success");
                    },
                    error : function(e) {
                        console.info("Error");
                    },
                    done : function(e) {
                        console.info("DONE");
                    }
                });
}

我有一个控制器方法:

    @ResponseBody
    @RequestMapping(value = "/sampleAddress", method = RequestMethod.POST)
    public String wileyBillShippAddressFormValidation() {
        System.out.println(this.getClass());
        return "{}";
    }

如果我调用 mm(),则会收到以下错误:

GET https://localhost:9002/address/sampleAddress 404 (Not Found)

有人能解释一下为什么吗?又花了2个小时没看懂。

【问题讨论】:

  • 您确定要发出 POST 请求吗?该错误似乎表明您正在发出 GET 请求。
  • 是的,我确定。所以我问了这个问题))
  • 您可以使用Post 简写来确保 POST 请求确实发生了吗?正如 Felix King 所说,您的错误清楚地表明发出了 GET 请求。
  • @Romulo,谢谢你的回答。我从 $.ajax 更改为 $.post ,现在出现了更容易理解的错误。它是“POST https ... 404 Not Found”然后我改回ajax 并且“GET https ... 404 Not Found”又得到了。我不明白为什么会这样……
  • 您使用的是什么版本的 jQuery? method: 选项是在 1.9 中添加的,在此之前它应该是 type:

标签: jquery ajax


【解决方案1】:

问题已解决。在调试 Spring 过滤器后,我了解到 request 在 java 代码中没有一些参数。然后我改变了ajax如下

mm();
function mm() {
                $.ajax({
                    method : "POST",
                    url : "/address/sampleAddress",
                    dataType : "json",
                    timeout : 100000,
                    success : function(data) {
                        console.info("Success");
                    },
                    error : function(e) {
                        console.info("Error");
                    },
                    done : function(e) {
                        console.info("DONE");
                    }
                });
}

即删除contentType : "application/json"之后请求通过

【讨论】:

    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2018-05-24
    • 1970-01-01
    相关资源
    最近更新 更多