在我的HTML中,我有这个按钮:
<button id="download_button">Confirmer</button>
在我的Js中,我有这种方法:
$("#download_button").click(function() {
downloadMes();
});
downloadMes: function(){
var sources = this.map.getLayers().getArray().map(function(layer){
var source_url = layer.getSource().getUrls()[0];
var layer_name = layer.getSource().getParams()['LAYERS'];
var request = '?VERSION=1.1.0&OUTPUTFORMAT=CSV';
if(_.includes(this.downloadSources, source_url)){
return source_url + request + '&TYPENAME=' + layer_name;
}
},this);
var sourcesToCall = _.compact(sources);
var jsonfile= {json:JSON.stringify(sourcesToCall)};
$.ajax({
type:'POST',
url: "/downloadCSV",
data: jsonfile,
dataType: "json"
});
}
我创建了一个Spring Controller - &gt; DownloadCSVController:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class DownloadCSVController {
@RequestMapping(value = "/downloadCSV", method = RequestMethod.GET)
public void getCSV(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException{
System.out.println("DEBUT TELECHARGEMENT");
// SOME LOGIC
System.out.println("FIN TELECHARGEMENT");
}
}
当我点击我的按钮时,我有这个错误:
POST http://localhost:8080/downloadCSV 404(Introuvable)
有什么问题?
答案 0 :(得分:1)
将你的ajax调用改为像这样的GET方法
$.ajax({
type:'GEt',
url: "/downloadCSV",
data: jsonfile,
dataType: "json"
});
或更改弹簧控制器方法发布
@RequestMapping(value = "/downloadCSV", method = RequestMethod.POST)
public void getCSV(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException{
System.out.println("DEBUT TELECHARGEMENT");
// SOME LOGIC
System.out.println("FIN TELECHARGEMENT");
}
PS:如果您的项目有一些根文件夹并且基于调度程序servlet url映射也在请求类似localhost时指定了它的名称:8080 / yourproject / downloadCSV
答案 1 :(得分:0)
您尝试发布数据以获取网址。所以改变你的控制器并尝试这个
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class DownloadCSVController {
@RequestMapping(value = "/downloadCSV", method = RequestMethod.POST)
public void getCSV(HttpServletRequest request, HttpServletResponse response, @RequestBody ModelMap model) throws IOException{
System.out.println("DEBUT TELECHARGEMENT");
// SOME LOGIC
System.out.println("FIN TELECHARGEMENT");
}
}