【问题标题】:sending json object using $.ajax doesnt get to servlet使用 $.ajax 发送 json 对象不会到达 servlet
【发布时间】:2014-06-10 21:20:29
【问题描述】:

我正在尝试将带有 Ajax(POST) 的 json 对象发送到我的 Servlet, Ajax 好像没有被发送到servlet,意味着doPost 函数dosent 运行。 在发送 Ajax 之后,我如何从中获取数据并打印到控制台。 谢谢。

index.jsp

<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
<h2>Welcome please fill in the details, and then click Submit</h2>
<hr/>

<input value="Submit2" type="submit" onclick="submitform()">
<script type="text/javascript">
function submitform() {
      alert('sending json');

      $.ajax({
            type: 'POST',
            url: '/MyServlet',
            data: JSON.stringify({name:"hod"}),
            success: function(msg){
                alert('wow' + msg);
            }
        });

      alert('done json');

    }

</script>

</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WebApp-01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.srk.pkg.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>
</web-app>

MyServlet.java:

package com.srk.pkg;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
/*
 * Servlet implementation class MyServlet
 */
public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public MyServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("doGet");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost");
    // get the object and print to console
    }

}

【问题讨论】:

  • 您的浏览器有一个控制台,用于记录 javascript 执行和 HTTP 请求。检查它以获取更多详细信息。

标签: java jquery ajax json servlets


【解决方案1】:

实际上您需要做的是使用 jquery 或 javascript 将表单数据放入 javascript 对象中。如果您的数据是单个表单元素,例如名字然后使用 var firstName = document.getElementById('firstName').value() 从表单元素中获取值。

如果您的表单数据是多个表单元素,则收集您的数据并将其以数组左右的形式存储在 javascript 对象中。

您的 HTML 页面未包含 &lt;form&gt; 标记,因此 DOM 不会在您的提交中推送您的数据,因此不会向您的 ajax 调用发送数据。

在您的 ajax 调用中,您应该将 Content-type 标头设置为接受 Json 对象。我看到你正在对你的对象进行字符串化,这很好。

您的 javascript 对象将被发布到服务器,一旦您需要 Json 到 java 映射器来创建对象映射。如果您使用的是 struts,那么存在一个 Json 到 java 映射器。映射的其他解决方案是 GSON 或 Jackson。

你的服务器函数应该返回一个你想要返回的对象/字符串。您可以在变量 msg 中的成功回调函数中访问该数据,因为您在那里拥有它。如果您返回一个对象,例如学生可以打印 console.log(msg.firstname) 或打印返回的字符串。

我希望这对您有所帮助,它是有关方法以及您需要研究和实施的内容的指南。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2011-09-19
    • 2022-06-14
    • 1970-01-01
    相关资源
    最近更新 更多