struts 接收 jquery带参数ajax请求

 

$("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代码:

 

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就行,即使是代码全改了;

[quote] data : "productType.id=productTypeId", [/quote]

data : {'productType.id':productTypeId}

一般这么用,如果还是不能获取,建议你使用modenDriven,或者是简单地在action里get,set

ajax提交时不允许你那种给对象属性赋值的,有两种解决办法。
1、笨方法,得到所有的参数,在后台action里分别获取。这个参数你可以放在url里也可以以参数形式传。
$.post(url,{ id:productTypeId },function(data){
//data就是josn返回的信息 你可以在这处理 这个是回调函数
});
2 、将你的参数放在strut2标签里 用form异步提交
var saveData = $("#formid").formSerialize();
你的Ajax参数打他就是saveData
说这么多 有点儿复杂 你可以研究一下。至于你的找个小问题最简单的办法就是将参数直接写在url里,在action里直接定义属性,给个seter和geter方法就可以得到了