1

this fnction is when clicked button,

it just insert 1row to database.

insert row is perfectly, but...

problem is always return 'error alert'.

'success alert' is never return.

how to do.

ps. I'm sorry to my english is not good.

$(function(){
    $("[name='replyWrite']").click(function(){
        var text = $("textarea").val();
        $.ajax({
            url: "http://localhost:8080/replyWrite",
            type: "POST",
            data: "no=100&id=test&comment=this is test",
            dataType: "text",
            cache: "false",
            success: function(){
                alert("Success");
                // something to do
            },
            error: function(data, textStatus, errorThrown){
                alert("error\n" + data + ", " + textStatus + ", " + errorThrown);
            }
        });
    });
});

And replyWrite code is..

@RequestMapping(value="/replyWrite")
public String replyWrite(@RequestParam(required=true)int no
                        , @RequestParam(required=true)String id
                        , @RequestParam(required=true)String comment){
    return boardDaoJdbc.replyWrite(id, no, comment);
}



public String replyWrite(String id, int no, String comment){
    // it will be return "1"
    return Integer.toString(this.jdbcTemplate.update("INSERT INTO testz_board_reply(id, comment, linked_idx, write_date) VALUES (?, ?, ?, DATE_FORMAT(now(), '%Y-%c-%d %H:%i')) ",
                            id, comment, no));
}

2 Answers 2

2

If you are using ajax you need to use @ResponseBody annotation.

so your mapping should be like this:

@RequestMapping(value="/replyWrite", method=RequestMethod.POST)
@ResponseBody
public String replyWrite(@RequestParam(required=true)int no
                        , @RequestParam(required=true)String id
                        , @RequestParam(required=true)String comment){
    return boardDaoJdbc.replyWrite(id, no, comment);
}

Now on my client side:

<script src="../scripts/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){

    $("#replyWrite").click(function () {
        var text = $("textarea").val();
        var request = "no=100&id=test&comment=this is test",
        $.post("replyWrite", request,
                   function(data) {
                    alert(data);
                   });
        });
      });
</script>

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is probably with your URL: http://localhost:8080/replyWrite. If you've deployed your WAR application under some context path, you should use this context path in URL.

For example - Tomcat has ROOT folder inside webapps - only this "webapp" has empty context path. If your WAR is example.war, your URL should be http://localhost:8080/example/replyWrite.

Common way to handle this problem is to add a little bit of server-side processing to your JavaScript - to generate the URL (instead of hardcoding it).

For example with JSP use:

$.ajax({
    url: "<c:url value="/replyWrite" />",
    ...

regards Grzegorz Grzybek

1 Comment

what would you do when you putting all your AJAX javascript in separate js files? Will this work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.