javascript - 如何访问使用 get 方法发送的参数?

标签 javascript java jquery servlets

我正在尝试将位置数据从 javascript 发送到我的 servlet 并及时将其存储在数据库中。我一直获取 null 如何访问我的参数?

NewFile.html

<script>
    $(document).on("click", "#somebutton", function() {
        if (navigator.geolocation) {

            navigator.geolocation.watchPosition(showPosition);
        } else {
            x.innerHTML = "Geolocation is not supported by this browser.";
        }

        function showPosition(position) {
            console.log(position)

            var params = {
                lat : position.coords.latitude,
                lng : position.coords.longittude
            };

            $.get("someservlet", $.param(params), function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
                // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
            });

        }

    });
</script>

servlet.java

public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String text = "some text";
       String params= request.getParameter("params");
        System.out.println(params);
    }

}

最佳答案

您的 Servlet:

public class servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String text = "some text";
        System.out.println(String.format("Lat: %s Long: %s", request.getParameter("lat"), request.getParameter("lng")));
    }

}

建议您关注naming conventions :

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

关于javascript - 如何访问使用 get 方法发送的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59679671/

相关文章:

javascript - 如何预加载带有广告的外部JS文件?

javascript - 将数据传递给 nuxt fetch,存储调度不起作用

javascript - 这个javascript代码可以优化吗?

java - 将对象从池传递到可运行的类

javascript - Jquery 菜单在移动浏览器中不起作用

javascript - sqlite 检查提供的数据是否存在,则警报存在,否则警报不存在

javascript - d3.js 不读取 JSON (d3.js + dc.js + crossfilter)

java - 服务请求后停止 HttpServer

java - 如何在recyclerview中显示图片?

javascript - jQuery 切换功能使文本变形