Java 使用$.ajax发送json对象无法访问servlet

Java 使用$.ajax发送json对象无法访问servlet,java,jquery,ajax,json,servlets,Java,Jquery,Ajax,Json,Servlets,我正试图通过Ajax(POST)将json对象发送到我的Servlet, Ajax似乎没有发送到servlet,这意味着doPost函数没有运行。 在Ajax被发送之后,我如何从中获取数据并打印到控制台。 谢谢 index.jsp: <%@ page language="java" contentType="text/html; charset=windows-1255" pageEncoding="windows-1255"%> <!DOCTYPE html PUBL

我正试图通过Ajax(POST)将json对象发送到我的Servlet, Ajax似乎没有发送到servlet,这意味着doPost函数没有运行。 在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>

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

如果表单数据是多个表单元素,则收集数据并以数组等形式存储在javascript对象中

HTML页面没有包含
标记,因此DOM不会将您的数据推送到submit中,因此不会向ajax调用发送数据

在ajax调用中,应该将Content-type头设置为接受Json对象。我看你在把你的目标串起来,这很好

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

您的服务器函数应该返回您想要返回的对象/字符串。该数据可以在变量msg中的success回调函数中访问。如果您返回一个对象,例如Student,您可以打印console.log(msg.firstname)或打印返回的字符串


我希望这对您有所帮助,并为您研究和实施的方法和内容提供指导

您的浏览器有一个控制台,用于记录javascript执行和HTTP请求。查看更多详细信息。
<?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>
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
    }

}