struts 接收 jquery带参数ajax请求



struts 接收 jquery带参数ajax请求

  1. $(“input[name=#delete]“).click(function(e) {
  2.         var productTypeId = parseInt($(this).parent().parent()
  3.                 .children(“.productTypeId”).html());
  4.         // var json = {
  5.         // parameter : productTypeId
  6.         // }
  7.         // var strjson = JSON.stringify(json);
  8.         alert(productTypeId);
  9.         // var url = “controller/product/productType!deleteProductType.action”;
  10.         // var jsonObject = {
  11.         // id : productTypeId
  12.         // }; // JSON对象
  13.         // var strjson = JSON.stringify(jsonObject); // 将JSON对象转变成JSON格式的字符串
  14.         // $.post(url, {
  15.         // json : strjson
  16.         // }, callback);
  17.         $.ajax({
  18.                     url : “controller/product/productType!deleteProductType.action”,
  19.                     // dataType : “text”,
  20.                     // 这个为什么要注释掉呢?
  21.                     data : “productType.id=productTypeId”,
  22.                     type : “post”,
  23.                     success : function(data) {
  24.                         alert(“删除成功”);
  25.                     },
  26.                     complete : function(XMLHttpRequest, textStatus) {
  27.                         // closeWindow();
  28.                         alert(this);
  29.                     },
  30.                     error : function(data) {
  31.                         alert(“删除失败”);
  32.                     }
  33.                 });
  34.     });
$("input[name=#delete]").click(function(e) {
		var productTypeId = parseInt($(this).parent().parent()
				.children(".productTypeId").html());
		// var json = {
		// parameter : productTypeId
		// }
		// var strjson = JSON.stringify(json);
		alert(productTypeId);

		// var url = "controller/product/productType!deleteProductType.action";
		// var jsonObject = {
		// id : productTypeId
		// }; // JSON对象
		// var strjson = JSON.stringify(jsonObject); // 将JSON对象转变成JSON格式的字符串
		// $.post(url, {
		// json : strjson
		// }, callback);

		$.ajax({
					url : "controller/product/productType!deleteProductType.action",
					// dataType : "text",
					// 这个为什么要注释掉呢?
					data : "productType.id=productTypeId",
					type : "post",
					success : function(data) {
						alert("删除成功");
					},
					complete : function(XMLHttpRequest, textStatus) {
						// closeWindow();
						alert(this);
					},
					error : function(data) {
						alert("删除失败");
					}
				});
	});

我要把productTypeId传递给struts2 的action,action接收这个数据进行删除操作,下面是action代码:

 

Java代码 复制代码 收藏代码
  1. public String deleteProductType() {
  2.         // System.out.println(productType.getId());
  3.         // JSONObject jsonObject = new JSONObject();
  4.         // int productTypeId = Integer.parseInt(jsonObject.getJSONObject(json)
  5.         // .getString(“parameter”));
  6.         // productTypeService.delete(ProductType.class, productTypeId);
  7.         // System.out.println(productTypeId);
  8.         // queryPage();
  9.         // return “deleteProductType”;
  10.         productTypeService.delete(ProductType.class, productType.getId());
  11.         System.out.println(productType.getId());
  12.         return null;
  13.     }
public String deleteProductType() {

		// System.out.println(productType.getId());
		// JSONObject jsonObject = new JSONObject();
		// int productTypeId = Integer.parseInt(jsonObject.getJSONObject(json)
		// .getString("parameter"));
		// productTypeService.delete(ProductType.class, productTypeId);
		// System.out.println(productTypeId);
		// queryPage();
		// return "deleteProductType";
		productTypeService.delete(ProductType.class, productType.getId());
		System.out.println(productType.getId());
		return null;
	}

控制台输出的productType.getId()为null;请朋友费心帮忙一下,只要可以让我用jquery把数据发送给action就行

 

采纳的答案

引用
data : “productType.id=productTypeId”,

data : {‘productType.id’:productTypeId}
一般这么用,如果还是不能获取,建议你使用modenDriven,或者是简单地在action里get,set