0

我在服务器上托管了两个 Web 应用程序。从一个我试图做 $.post 到第二个应用程序(spring mvc3)。我能够成功点击第二个应用程序的 url,但我没有得到 ajax 调用的响应。

App1 JS 代码 (http://localhost:7777/app1/test.html) -

var serialisedFormData = $("form").serialize();
 $.post("http://localhost:8080/app2/doSomething", serialisedFormData, function (data) {
        alert("job done");        
    }, "json");

App2 Java 代码 -

    @RequestMapping(value = "/doSomething")
        public @ResponseBody String doSomething(HttpServletRequest request, 
 @RequestParam("d") String data) {

            try {
                   ... do something here ...
                   return "done";
            }
            catch (Exception e) {
                logger.debug("Exception occured when doing something", e);
                return "failure";
            }
        }
4

1 回答 1

0

我通过从我的 jquery post call 中删除“json”类型来实现它。

$.post("http://localhost:8080/app2/doSomething", serialisedFormData, function (data) {
        alert("job done");        
    });

并在我的回复中添加标题

@RequestMapping(value = "/doSomething")
        public @ResponseBody String doSomething(HttpServletRequest request, HttpServletResponse response, @RequestParam("d") String data) 
     {

            try {
                   ... do something here ...
                   response.setHeader("Access-Control-Allow-Origin", "*");
                   return "done";
            }
            catch (Exception e) {
                logger.debug("Exception occured when doing something", e);
                return "failure";
            }
        }
于 2012-10-04T09:05:52.543 回答