Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Java 带Spring的ajax_Java_Ajax_Spring_Jquery - Fatal编程技术网

Java 带Spring的ajax

Java 带Spring的ajax,java,ajax,spring,jquery,Java,Ajax,Spring,Jquery,帮助我编写SpringWeb应用程序来读取文件并显示为html页面 jQuery(document).ready(function($){ $.ajax({ url : "../xml.txt", type:"POST", dataType: "text", success : function (data) { $('<pre />').t

帮助我编写SpringWeb应用程序来读取文件并显示为html页面

jQuery(document).ready(function($){
        $.ajax({
            url : "../xml.txt",
            type:"POST", 
            dataType: "text",
            success : function (data) {
               $('<pre />').text(data).appendTo('div');
               window.location.href=contextPath+"http://localhost:8080/subin.html"
            }
        });
      });

您最好不要在AJAX请求中公开文件的真实路径。您可以保留该抽象,让控制器方法解析实际路径并加载文件

比如:

jQuery(document).ready(function($){
        $.ajax({
            url : "/data?name=xml.txt", // Abstract path and filename
            type:"GET", 
            dataType: "text",
            success : function (data) {
               $('<pre />').text(data).appendTo('div');
               window.location.href=contextPath+"http://localhost:8080/subin.html"
            }
        });
      });
该示例使用了
FileInputStream
,但根据您的需求,您可以从不同类型的位置加载文件,例如类路径或URI。还要注意,它使用GET而不是POST


希望能有帮助。

你的
控制器在哪里
代码?@Bk Santiago我贴在这里了
jQuery(document).ready(function($){
        $.ajax({
            url : "/data?name=xml.txt", // Abstract path and filename
            type:"GET", 
            dataType: "text",
            success : function (data) {
               $('<pre />').text(data).appendTo('div');
               window.location.href=contextPath+"http://localhost:8080/subin.html"
            }
        });
      });
  @RequestMapping(value = "/data", params="name", method = RequestMethod.GET)
  public @ResponseBody String getData(@RequestParam(value="name") String name) {
    InputStream in = new FileInputStream("/real/path/" + name);
    String contents = IOUtils.toString(in, "UTF-8");
    return contents;
  }