gpt4 book ai didi

jquery - 从 servlet 返回 JSON

转载 作者:行者123 更新时间:2023-12-03 22:04:52 25 4
gpt4 key购买 nike

这是一个非常基本的请求响应测试。浏览器使用 jQuery $.ajax API 向 servlet 发送“hello from browser”,servlet 接收此消息,然后使用 org.json.simple 库创建一个 JSON 对象,并向浏览器发送回带有消息“hello from server”的 JSON 响应。

我在本地主机上运行它,假设我的 IP 地址是 123.123.12.123,平台是 Ubuntu,服务器是 Tomcat 6.0,在 Eclipse IDE 中运行。

测试1.我从Eclipse启动服务器,打开Firefox,输入http://localhost:8080/myproject/test.jsp ,可以看到servlet收到消息,浏览器收到响应,测试通过。

测试2.服务器仍然在Ubuntu的Eclipse上运行,我从VirtualBox启动Windows 7客户机,并在Windows 7中启动Firefox浏览器,输入http://123.123.12.123:8080/myproject/test.jsp ,按我的预期工作,测试通过。

测试3.服务器仍在Ubuntu的Eclipse上运行,打开Internet Explorer 9浏览器,给它地址http://123.123.12.123:8080/myproject/test.jsp什么也没发生。调试给了我

响应 HTTP/1.1 200 OK

响应正文{"message":"来自服务器的问候"}

test.jsp 是

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" type="text/javascript"></script><script type="text/javascript" src="release/js/libs/json2.js"></script><script>$(document).ready(function(){    var request = ({"message":'Hello from browser'});    var jsonobj=JSON.stringify(request);    $.ajax({        data: {para:jsonobj},        dataType: 'json',        url: './TestServlet',        type: 'POST',        success: function(jsonObj){            alert(jsonObj.message);             },        error: function() {            alert('Ajax readyState: '+xhr.readyState+'\nstatus: '+xhr.status + ' ' + err);        }    });});</script><body></body></html>

servlet 代码是

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.json.simple.JSONObject;import org.json.simple.JSONValue;/** * Servlet implementation class TestServlet */public class TestServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    /**     * @see HttpServlet#HttpServlet()     */    public TestServlet() {        super();    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("utf8");        response.setCharacterEncoding("utf8");        response.setContentType("application/json");         PrintWriter out = response.getWriter();         JSONObject jsonObj = (JSONObject) JSONValue.parse(request.getParameter("para"));        System.out.println(jsonObj.get("message"));                 JSONObject obj = new JSONObject();        obj.put("message", "hello from server");        out.print(obj);    }}

更新:

通过更改仔细查看后

 error: function() {            alert('Ajax readyState: '+xhr.readyState+'\nstatus: '+xhr.status + ' ' + err);}

error: function(xhr,err) {            alert('Ajax readyState: '+xhr.readyState+'\nstatus: '+xhr.status + ' ' + err);        }

我收到警报 readyState:0 和 status:0。但我可以在响应正文中看到 {"message":"hello from server"}响应头是

Key ValueResponse    HTTP/1.1 200 OK

最佳答案

IE 会积极缓存 AJAX 请求(无论如何,比 Firefox、Chrome 和 Safari 缓存的要多)。有时您需要在请求时设置缓存头 Controller 。就像cache:false一样。我尝试像这样修复你的代码

request.setCharacterEncoding("utf8");
//response.setCharacterEncoding("utf8");
response.setContentType("application/json");
PrintWriter out = response.getWriter();
JSONObject jsonObj = (JSONObject) JSONValue.parse(request.getParameter("para"));
System.out.println(jsonObj.get("message"));
JSONObject obj = new JSONObject();
obj.put("message", "hello from server");
out.print(obj.toString());

我从 application/json 更改了您的响应内容类型; charset=utf8application/json 并且有效。

关于jquery - 从 servlet 返回 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com