【问题标题】:Ajax call URL cannot found @RequestMapping value on Spring serverAjax 调用 URL 在 Spring 服务器上找不到 @RequestMapping 值
【发布时间】:2016-09-26 06:19:04
【问题描述】:

我是使用 Ajax 调用和 Spring 服务器的初学者。我想将一些 JSON 格式的数据从 html 发送到 Spring 服务器。这是我的 Ajax 调用代码:

function sendToServer(){
$.ajax({
    type: "POST",
    url:"/maricobalagi/getJsonByMap",
    dataType: "json",
    data:"{formdata}",
    //success and error message
    success: function() {
         alert("The page has been successfully loaded");
      },

     error: function() {
        alert("An error occurred");
      },
});
}

但是我的 Ajax 调用 URL 有问题,它找不到 "/getJsonByMap" 值。控制台日志中有错误消息"POST http://localhost:8092/maricobalagi/getJsonByMap 404 (Not Found)"。这是我的服务器代码:

public class Controller{
@RequestMapping(value = "/getJsonByMap" , method = RequestMethod.POST)
public @ResponseBody Map post(@RequestBody final Map JSONmap){

  //myCode

}
}

我只有 web.xml,这是我的 web.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Generic</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这是我的项目文件夹: Project Folder Screenshot

【问题讨论】:

  • 你能把你的web.xmlwebApp.xml文件内容贴在这里吗?
  • 我已经编辑了帖子,但我没有任何 webApp.xml
  • 您的web.xml 中没有定义任何&lt;servlet-mapping&gt; 吗?
  • 抱歉,我的 web.xml 中没有包含任何 ,因为总是有一个“cvc-complex-type.2.4.b: The content of element 'servlet-mapping'不完整。应为“{“http://xmlns.jcp.org/xml/ns/javaee”:servlet-name}之一。”错误信息。

标签: javascript php jquery ajax spring


【解决方案1】:

使用 @RestController 注释您的控制器类,并在方法中返回一些内容。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 2013-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
相关资源
最近更新 更多