4Manuals

  • PDF Cloud HOME

Spring MVC @ModelAttribute未填充AJAX发布请求 Download

    春季启动-Couchbase AbstractCouchbaseConfiguration-如何避免覆盖不必要的方法 我不知道(未解决的编译问题:) 加载数据并推送到未显示的RecyclerView中 调用Model类的函数时出现java.lang.NullPointerException 字符串数组在for循环中初始化时忽略索引0 通过dropzone laravel通过电子邮件发送多个图像 需要建议在Java中使用哪个数据库/存储来存储RaftLogs(实现Raft Consensus Algorithm) Flutter:Java使用或覆盖已弃用的API @ EnableOAuth2Sso和@EnableResourceServer(同一应用程序中的客户端和资源行为) 中断可运行线程的问题

当我尝试通过Ajax Post请求向控制器发送数据时,不会填充我的@ModelAttribute对象。但是,当我使用表单发送值时,它可以正常工作。请检查下面的代码,

表格

<form action="#" id="productForm" th:action="@{/save}" th:object="${product}" method="POST">
    <table>
        <tbody>
            <tr>
                <td>Name:</td>
                <td><input id="name" type="text" th:field="*{name}" /></td>
            </tr>
            <tr>
                <td>Brand:</td>
                <td><input id="brand" type="text" th:field="*{brand}" /></td>
            </tr>
            <tr>
                <td>Made In:</td>
                <td><input id="madeIn" type="text" th:field="*{madeIn}" /></td>
            </tr>
            <tr>
                <td>Price:</td>
                <td><input id="price" type="text" th:field="*{price}" /></td>
            </tr>
        </tbody>
    </table>
    <br />
    <button type="submit">Save</button>
    <button type="button" onclick="saveWithAjaxPost();">Save with Ajax Post</button>
</form>

控制器

@RequestMapping(value = "/saveWithAjaxPost", method = RequestMethod.POST)
@ResponseBody
public ModelMap saveWithAjaxPost(@ModelAttribute Product product) {
    ModelMap model = new ModelMap();
    service.saveProduct(product);
    model.put("MESSAGE", "Success");
    return model;
}

JavaScript

function saveWithAjaxPost(){

    var params = $("#productForm").serialize();

    $.ajax({

        url:"/saveWithAjaxPost",
        type:"POST",
        contentType:"application/json",
        data:params,
        async:true,
        dataType:"json",
        success:function(response){
            $('#productsListDiv').empty();
            getAllProcucts();
        },
        error:function(response){
            alert(response.message);
        }
    });
}

序列化表单后,我得到"name=test_name&brand=test_brand&madeIn=test_made_in&price=1000"。

这是我的控制器在运行代码时得到的, enter image description here

这肯定是一个非常简单的错误,但我在这里无法弄清楚自己在做什么错...可能是什么问题?

0 个答案:

没有答案



Similar searches
    使用参数<this ['method']>失败 建议为tfjs-node-gpu使用哪种硬件? Samsung RFG29THD Refrigerator User Manual 如何在Ruby中删除多个命名文件 当两个应用程序同时访问一个localspark会话实例时发生错误。调用o2402.defaultParallelism时发生错误