Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.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/4/powerbi/2.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传递到servlet_Javascript_Java_Json_Jsp_Servlets - Fatal编程技术网

如何将数据从javascript传递到servlet

如何将数据从javascript传递到servlet,javascript,java,json,jsp,servlets,Javascript,Java,Json,Jsp,Servlets,我有一个JS,我将坐标从它发送到一个Servlet 在我的JS中,我从jsp页面获取坐标,如下所示: function main1() { $.ajax({ url: 'ServeltConnection', type: "GET", dataType: "json", data: { latitude: pos.lat, longitude: pos.lng, }, success: function(dat

我有一个JS,我将坐标从它发送到一个Servlet

在我的JS中,我从jsp页面获取坐标,如下所示:

 function main1() {
  $.ajax({
    url: 'ServeltConnection',
    type: "GET",
    dataType: "json",
    data: {
       latitude: pos.lat,
       longitude: pos.lng,

     },
    success: function(data){

    //call of  functions
    }
});
}     
我需要这些坐标,以便将它们发送到我的servlet中,以获得json格式的返回

在我的Servlet中:

 public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
        response.setContentType("application/json");
        PrintWriter out = new PrintWriter(response.getWriter(), true);
    List<Shop> shops;

    try{

    //code for connection with database
            shops=getShops(request.getParameter("latitude"),request.getParameter("longitude"));

        String json = new Gson().toJson(shops);
        response.getWriter().write(shops);
        db.close();

} catch (Exception ex) {
  out.println("Exception: " + ex.getMessage());
public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException、ServletException{
setContentType(“应用程序/json”);
PrintWriter out=新的PrintWriter(response.getWriter(),true);
列出店铺名单;
试一试{
//与数据库连接的代码
shops=getShops(request.getParameter(“纬度”)、request.getParameter(“经度”);
字符串json=new Gson().toJson(商店);
response.getWriter().write(商店);
db.close();
}捕获(例外情况除外){
out.println(“异常:+ex.getMessage());

当我给出具体的经度和纬度坐标时,我得到了正确的json,但当我试图从js中获取坐标时,它不起作用。我认为我从js中解析经度和纬度的方式是错误的。我搜索了解析json,但我做不到。你能帮我吗?

你必须在url上输入GET请求的参数,这样说:

url: 'ServeltConnection?latitude="+pos.lat+"&longitude="+pos.lng+"'

还有,别忘了删除
'data'
参数

你的意思是将你的数据“传递”给servlet吗?是的,我的意思是,对不起!!我想获取我现在所在的坐标,并在最近的商店的地图上做标记。但是servlet似乎没有在shops=getShops(request.getParameter(“纬度”)中获取坐标,request.getParameter(“经度”);