jquery ajax servlet交互

jquery ajax servlet交互,所需的java包 jackson-all-1.9.2.jar、json-lib-2.4.jar、commons-lang-2.0.jar、ezmorph-1.0.6.jar、commons-beanutils.jar、commons-collections-3.2.1.jar...  两个js    jquery1.4.js、json2.js

如何ajax出现textStatus :406错误,很可能就是包忘记加了!

上代码

servlet:

package com.sunshine.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import net.sf.json.*;

public class JasonServlet extends HttpServlet {

/**
  * Constructor of the object.
  */
public JasonServlet() {
  super();
}

/**
  * Destruction of the servlet. <br>
  */
public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
}

/**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  PrintWriter out = response.getWriter();
  List<UserModel> list = new ArrayList<UserModel>();
  UserModel um = new UserModel();
  um.setId("1");
  um.setUsername("sss");
  um.setAge(222);
  list.add(um);
  Map<String, Object> modelMap = new HashMap<String, Object>(3);
  modelMap.put("total", "1");
  modelMap.put("data", list);
  modelMap.put("success", "true");
  JSONArray nn = JSONArray.fromObject(modelMap);
  out.print(nn);// 返给ajax请求
}

/**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to
  * post.
  *
  * @param request
  *            the request send by the client to the server
  * @param response
  *            the response send by the server to the client
  * @throws ServletException
  *             if an error occurred
  * @throws IOException
  *             if an error occurred
  */
public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  try {
   String userStr = readJSONString(request);// 得到requestContext
   JSONObject jsonObj = JSONObject.fromObject(userStr);// 转换成JSONObject
   System.out.println(jsonObj.getInt("id"));// 得到JSONObject的userId值
   System.out.println(jsonObj.getString("username"));
   System.out.println(jsonObj.getString("age"));
  
   JSONObject resultJSON = new JSONObject();// 构建一个JSONObject
   resultJSON.accumulate("errNum", 1);
   resultJSON.accumulate("errInfo", "成功");

   response.setContentType("application/x-json");// 需要设置ContentType为"application/x-json"
   PrintWriter out = response.getWriter();
   out.println(resultJSON.toString());// 向客户端输出JSONObject字符串
   out.flush();
   out.close();
  } catch (Exception e) {
   e.printStackTrace();
  }

}

/**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException
  *             if an error occurs
  */
public void init() throws ServletException {
  // Put your code here
}

public String readJSONString(HttpServletRequest request) {
  StringBuffer json = new StringBuffer();
  String line = null;
  try {
   BufferedReader reader = request.getReader();
   while ((line = reader.readLine()) != null) {
    json.append(line);
   }
  } catch (Exception e) {
   System.out.println(e.toString());
  }
  return json.toString();
}

}

js:

$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
  if (o[this.name]) {
   if (!o[this.name].push) {
    o[this.name] = [ o[this.name] ];
   }
   o[this.name].push(this.value || '');
  } else {
   o[this.name] = this.value || '';
  }
});
return o;
};

$(document).ready(
  function() {
   jQuery.ajax( {
    type : 'GET',
    url : 'jasonServlet',
    dataType: 'json',   //返回值类型
    success : function(data) {
    //if (data && data.success == "true") {
    if (data) {
    alert(data);
    $.each(data, function(i, item) {
     $('#info').html("共" + item.total + "条数据。<br/>"+item.success+"<br/>");
     // $.each(data.data, function(i, item) {
     $.each(item.data, function(i, item) {
       $('#info').append(
         "编号:" + item.id + ",姓名:" + item.username
           + ",年龄:" + item.age);
      });
     });
    
     }
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    }
   });
   $("#submit").click(function() {
    var jsonuserinfo = $.toJSON($('#form').serializeObject());
    alert(jsonuserinfo);
    jQuery.ajax( {
     type : 'POST',
     contentType : 'application/json',
     url : 'http://localhost:8081/spring_ajax/jasonServlet',
     data : jsonuserinfo,
     dataType : 'json',
     success : function(data) {
      alert("新增成功!");
     },
     error : function(data) {
      alert("error")
     }
    });
   });
  });

html:

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Spring MVC</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/index.js"></script>
</head>
<body>
<div id="info"></div>
<form action="add" method="post" id="form">
id:<input type="text" name="id"/>
name:<input type="text" name="username"/>
age:<input type="text" name="age"/>

<input type="button" value="提交" id="submit"/>
</form>
</body>
</html>

<input type="button" value="提交" id="submit"/>
</form>
</body>
</html>

### 关于ArcGIS License Server无法启动的解决方案 当遇到ArcGIS License Server无法启动的情况,可以从以下几个方面排查并解决问题: #### 1. **检查网络配置** 确保License Server所在的计算机能够被其他客户端正常访问。如果是在局域网环境中部署了ArcGIS Server Local,则需要确认该环境下的网络设置是否允许远程连接AO组件[^1]。 #### 2. **验证服务状态** 检查ArcGIS Server Object Manager (SOM) 的运行情况。通常情况下,在Host SOM机器上需将此服务更改为由本地系统账户登录,并重启相关服务来恢复其正常工作流程[^2]。 #### 3. **审查日志文件** 查看ArcGIS License Manager的日志记录,寻找任何可能指示错误原因的信息。这些日志可以帮助识别具体是什么阻止了许可服务器的成功初始化。 #### 4. **权限问题** 确认用于启动ArcGIS License Server的服务账号具有足够的权限执行所需操作。这包括但不限于读取/写入特定目录的权利以及与其他必要进程通信的能力。 #### 5. **软件版本兼容性** 保证所使用的ArcGIS产品及其依赖项之间存在良好的版本匹配度。不一致可能会导致意外行为完全失败激活license server的功能。 #### 示例代码片段:修改服务登录身份 以下是更改Windows服务登录凭据的一个简单PowerShell脚本例子: ```powershell $serviceName = "ArcGISServerObjectManager" $newUsername = ".\LocalSystemUser" # 替换为实际用户名 $newPassword = ConvertTo-SecureString "" -AsPlainText -Force Set-Service -Name $serviceName -StartupType Automatic New-ServiceCredential -ServiceName $serviceName -Account $newUsername -Password $newPassword Restart-Service -Name $serviceName ``` 上述脚本仅作为示范用途,请依据实际情况调整参数值后再实施。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值