简体   繁体   English

对Spring MVC控制器的Ajax调用将重定向页面而不是返回值

[英]Ajax call to Spring MVC controller redirects page instead of returning value

I'm a little bit confused. 我有点困惑。 I'm writing an MVC application and have a simple controller like this: 我正在编写一个MVC应用程序,并且有一个像这样的简单控制器:

@Controller
public class ProfileController {

final String DEFAULT_MALE_AVATAR = "../resources/graphics/avatarMan.PNG";
final String DEAULT_FEMALE_AVATAR = "../resources/graphics/avatarWoman.PNG";

@Autowired
UserService userService;

@RequestMapping(value = "/profile", method = RequestMethod.GET)
public String index() {

    return "user/profile";
}

@RequestMapping(value = "profile/getavatar", method = RequestMethod.GET)
public @ResponseBody String getLoggedUserAvatar() {

    String userMail = SecurityContextHolder.getContext()
            .getAuthentication().getName();

    User loggedUser;

    if (userMail != null) {
        loggedUser = userService.findUserByEmail(userMail);
        return loggedUser.getAvatar();
    } else {
        return DEFAULT_MALE_AVATAR;
    }
}

I've also got a simple js file called with "onload" in my body html tag while entering /profile section. 输入/ profile部分时,我的body html标签中还有一个名为“ onload”的简单js文件。

function init() {

var url = "profile/getavatar";

$.ajax({
    url : url
}).then(function(data) {
    avatarLink = data;
    loadAvatar(avatarLink);
});

function loadAvatar(avatarLink){
    $("#userAvatar").attr("src", avatarLink);
}

}

And for some strange reason I get ridirected to "profile/getavatar" and the page contains text with value returned by getLoggedUserAvatar(). 并且由于某些奇怪的原因,我被重定向到“ profile / getavatar”,并且页面包含具有由getLoggedUserAvatar()返回的值的文本。 The funny thing is I've also got some other controllers to other sections with almost the same js files and controllers - and they work like a charm. 有趣的是,我在其他部分也使用了几乎相同的js文件和控制器来使用其他一些控制器-它们的工作原理很吸引人。

What am I missing? 我想念什么?

I hope when you hit the URL directly, you are getting expected response. 希望您直接点击该URL时,得到预期的响应。 If that is not happening, then there is something else wrong. 如果这没有发生,则说明存在其他问题。 If you are getting proper response when you directly hit the url in browser, then try doing the below when doing the ajax call. 如果在浏览器中直接命中URL时得到正确的响应,则在进行ajax调用时尝试执行以下操作。 It passes the content type that is expecting back from the server. 它传递期望从服务器返回的内容类型。

function init() {

var url = "profile/getavatar";

$.ajax({
    url : url,
    dataType: "json"
}).then(function(data) {
    avatarLink = data;
    loadAvatar(avatarLink);
});

function loadAvatar(avatarLink){
    $("#userAvatar").attr("src", avatarLink);
}

}

If you are using spring 4, Please make sure that you have Jakson jars in your dependency library. 如果您使用的是Spring 4,请确保在依赖库中有Jakson jar。 framework will automatically pickup the content negotiator as JSON and will find for the Jakson jars in the background to transport JSON to server and get JSON data back from server 框架将自动将内容协商器作为JSON提取,并在后台查找Jakson jar,以将JSON传输到服务器并从服务器获取JSON数据

use JAXB jars , in case you need to handle XML as content negotiator. 使用JAXB jars,以防您需要将XML作为内容协商者来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM