Spring RESTful ajax会出错

当我将服务地址http://localhost:8080/mailservice/mail/name放入Web浏览器地址栏时,我会收到json中的响应,如下所示:

 ["name"] 

但是当我创建一个像这样的简单的html页面时:

     $( document ).ready(function() { doMagic(); }); function doMagic() { $.ajax({ type: 'GET', url: 'http://localhost:8080/mailservice/mail/name', contentType:"application/json", success: function () { console.log("ok"); }, error: function (request, status, error) { console.log(status); } }); }    

我的ajax总是遇到错误函数,如果我使用POST(不同的ajax代码与html,此处不存在)或GET,则无关紧要。

我的控制器看起来像这样:

 @Controller @RequestMapping("/mail") public class MailController { @Autowired MailService mailService; @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces={"application/json"}) public @ResponseBody List getMovie(@PathVariable String name) { List lista = Arrays.asList(name); return lista; } @RequestMapping(value = "/send", method = RequestMethod.POST, produces={"application/json","application/xml"}) @ResponseBody public String getDefaultMovie(Message message) { mailService.sendEmail(message); return "OK!"; } } 

来自萤火虫的信息:

 GET http://localhost:8080/mailservice/mail/name 200 OK 9ms Headers Response headers Content-Type application/json;charset=UTF-8 Date Sat, 12 Apr 2014 14:07:22 GMT Server GlassFish Server Open Source Edition 4.0 Transfer-Encoding chunked X-Powered-By Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.0 Java/Oracle Corporation/1.7) Request headers Accept */* Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 DNT 1 Host localhost:8080 Origin null User-Agent Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0 error 

我的web.xml

   Mail service  dispatcher  org.springframework.web.servlet.DispatcherServlet  1   dispatcher /*   

我的dispatcher-servlet.xml

                

我的pom.xml

  4.0.0 com.szymon mailservice war 0.0.1-SNAPSHOT mailservice Maven Webapp http://maven.apache.org  3.2.5.RELEASE    org.springframework spring-core ${spring.version}   org.springframework spring-web ${spring.version}   org.springframework spring-webmvc ${spring.version}   org.springframework spring-tx ${spring.version}   org.codehaus.jackson jackson-core-asl 1.9.12   org.codehaus.jackson jackson-mapper-asl 1.9.12   org.slf4j slf4j-api 1.5.6 jar   org.slf4j slf4j-simple 1.5.6   log4j log4j 1.2.16   javax.mail mail 1.4   org.codemonkey.simplejavamail simple-java-mail 2.1   javax.servlet servlet-api 2.5 provided   org.json json 20131018   junit junit 3.8.1 test    mailservice   

让别人帮忙:(

在你的ajax电话中,你能试试吗?

 ... ... url: '/mailservice/mail/name' ... ... 

并重新编译它,然后再试一次。 我认为这是问题因为原点是空的。

免责声明:这是从问题中提取的。

解决方案(部分)

我不得不更改html文件和web.xml以及dispatcher-servlet.xml

 $(function() { var url = 'http://localhost:8080/email/mail/send.jsonp?callback=jsonCallback'; $.ajax({ url: url, type: "GET", async: false, jsonpCallback: 'jsonCallback', contentType: "application/json", dataType: 'jsonp', error: function(a, b, c) { console.log(JSON.stringify(a)+","+b+","+c); }, success: function( response ) { console.log( response ); } }); }); 

调度员servlet.xml中

                

web.xml中:

  dispatcher *.json *.jsonp /*  

MyMapperClass.java

 package org.szymon.email.classes; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.view.json.MappingJacksonJsonView; public class MyMapperClass extends MappingJacksonJsonView { /** * Default content type. Overridable as bean property. */ public static final String DEFAULT_CONTENT_TYPE = "application/javascript"; @Override public String getContentType() { return DEFAULT_CONTENT_TYPE; } /** * Prepares the view given the specified model, merging it with static * attributes and a RequestContext attribute, if necessary. * Delegates to renderMergedOutputModel for the actual rendering. * @see #renderMergedOutputModel */ @Override public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { if("GET".equals(request.getMethod().toUpperCase())) { @SuppressWarnings("unchecked") Map params = request.getParameterMap(); if(params.containsKey("callback")) { response.getOutputStream().write(new String(params.get("callback")[0] + "(").getBytes()); super.render(model, request, response); response.getOutputStream().write(new String(");").getBytes()); response.setContentType("application/javascript"); } else { super.render(model, request, response); } } else { super.render(model, request, response); } } }