Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 避免在浏览器上显示Spring@ResponseBody返回的数据_Javascript_Jquery_Http_Spring Mvc_Url - Fatal编程技术网

Javascript 避免在浏览器上显示Spring@ResponseBody返回的数据

Javascript 避免在浏览器上显示Spring@ResponseBody返回的数据,javascript,jquery,http,spring-mvc,url,Javascript,Jquery,Http,Spring Mvc,Url,好的,现在我在html页面上显示一些数据。我使用了jqueryajax调用,它从springmvc应用程序上的特定url获取数据并返回数据,然后使用javascript在表中显示数据 @RequestMapping(value = "/students", method = RequestMethod.GET) @ResponseBody @JsonView(Views.PublicView.class) public ArrayList<Student> getAdminsList

好的,现在我在html页面上显示一些数据。我使用了jqueryajax调用,它从springmvc应用程序上的特定url获取数据并返回数据,然后使用javascript在表中显示数据

@RequestMapping(value = "/students", method = RequestMethod.GET)
@ResponseBody
@JsonView(Views.PublicView.class)
public ArrayList<Student> getAdminsList(HttpSession session) {
    //return data
}
@RequestMapping(value=“/students”,method=RequestMethod.GET)
@应答器
@JsonView(Views.PublicView.class)
公共阵列列表getAdminsList(HttpSession会话){
//返回数据
}
现在,我的问题是,我通过ajax获得所需的数据,但如果我只通过url,我确实会在浏览器中显示数据:www.example.com/students-将显示JSON数据


是否有任何方法可以避免不在浏览器中而仅在ajax调用中显示此数据?

可能感觉像是一个问题[@JsonView(Views.PublicView.class)]。你可以查看PublicView.class

阅读说明。链接:

@RequestMapping(value=“/accounts”,method=RequestMethod.GET)
@JsonView(View.Accounts.class)
public List getAccounts()引发JsonProcessingException{
List accounts=accountService.findAccounts();
归还账户;
}

无论如何,我在谷歌搜索了很多遍后才找到了答案,我想分享它会是一件好事。因此,这个限制的基本逻辑是,无论何时从ajax发出调用,都会向请求附加一个名为X-request-With、值为
XMLHttpRequest
的特殊头。我唯一要做的就是在我的控制器上检查这个参数

@RequestMapping(value = "/accounts", method = RequestMethod.GET)
@JsonView(View.Accounts.class)
public List<Account> getAccounts() throws JsonProcessingException {
        if(!"XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
            log.info("Request is from browser.");
            return null;
        }
    List<Account> accounts = accountService.findAccounts();

    return accounts;
}
@RequestMapping(value=“/accounts”,method=RequestMethod.GET)
@JsonView(View.Accounts.class)

公共列表

非常感谢您提供的信息,但不幸的是我不为自己工作,而且我认为它与JsonView.Ok没有任何关系。我认为您可以对web应用程序进行精细化处理。除息的
@RequestMapping(value = "/accounts", method = RequestMethod.GET)
@JsonView(View.Accounts.class)
public List<Account> getAccounts() throws JsonProcessingException {
        if(!"XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
            log.info("Request is from browser.");
            return null;
        }
    List<Account> accounts = accountService.findAccounts();

    return accounts;
}