When sending Ajax communication url, can't you send the query string together?

Asked 2 years ago, Updated 2 years ago, 52 views

Hello! I am developing a movie reservation program using the spring MVC pattern

 **/* Press the Register Rating button to register comments! Non-login -> Induce Login Pop-up Window 
                                    When logged in -> moviescore insert!
                     At the same time as the insert, select the movies core and spray it on the screen!
      */**

    $("#btn_register").click(function(){
            var currentScore = $('#div_rating').raty('score'); // horoscope
            var cheklog = $("#loginid").val(); // Login ID
            var ms_mid = $("#ms_mid").val(); // Get movie m_number 

            if(cheklog == null || cheklog==""){
                alert("Not logged in!");
            }
            else{
                //alert ("You're logged in?");
                var params =$("#formId").serialize();
                $.ajax({
                    url:"detailMovie.com?m_number='"+ms_mid+"'",
                    data:params,
                    success:function(data){ 
                        alert(data);
                    }
                });Enter the code here`
                //$("#formId").attr("action","insertMovieScore.com");

            }

        });

The reason why you have to do this part of url:url:"detailMovie.com?m_number='"+ms_mid+"""" is because When you do detailMovie.com, the controller requests the m_number parameter fee (because it extracts the data that fits the m_number with the where conditional statement when querying db) I am trying to update my registered rating right below when I register my rating by using Ajax without moving the page. This part is blocked right now ㅠ 이렇게 If I do this, the rating can be inserted, but when I select it and bring it back, the m_number is null in the updateMovie.com section, so I can't extract the information from the detailed page I don't know how to deal with this.Help me

This is the part of the controller that comes when you request detailMove.com,

@RequestMapping("/detailMovie.com")
public ModelAndView getMovie(@RequestParam(value="m_number", defaultValue="1") int m_number,HttpServletRequest request,HttpServletResponse response, HttpSession session){
    ModelAndView mav = new ModelAndView();
    System.out.println ("detail1: "+request.getAttribute("m_number"));
    if(request.getAttribute("m_number")!=null && !request.getAttribute("m_number").equals(""))
    {
        //System.out.println ("before the error");
        m_number = Integer.parseInt(request.getAttribute("m_number").toString());
        System.out.println ("detail2: "+m_number");
    }


    mav.addObject("m",dao.getMovie(m_number));
    return mav;
}

This is the part of the controller that comes when you do insertMovieScore.com!

@RequestMapping("/insertMovieScore.com")
public ModelAndView submit(MovieScoreVo ms,HttpServletRequest request,HttpSession session,HttpServletResponse response){
    ModelAndView mav = new ModelAndView("redirect:/detailMovie.com");
    int re = dao.insertMovieScore(ms);

    request.setAttribute("m_number", ms.getMs_mid()); 

    List<MovieScoreVo> list = dao.listMovieScore();
    String json = "[";
    The part where I make and send Json

        json += "]";
    }

    try{
        response.setContentType("text/plain"); //plainjson type1!!!!
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }catch(Exception e){System.out.println(e.getMessage());}


    return mav;
}

javascript ajax jquery spring

2022-09-22 14:39

1 Answers

It's going to work out, but it's weird.

Just in case

url:"detailMovie.com?m_number='"+ms_mid+"'",

This part

url: "detailMovie.com?m_number=" + ms_mid,

Fix it like this.

Please refer to the attached source I tested.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="/resources/js/jquery-1.12.4.js"></script>
</head>
<body>
<h2>ajax test</h2>
<p>watch the console</p>
<p>response: <span style="color: red" id="result"></span></p>
<script>
$.ajax({
    url: '/hello.data?a=1',
    data: 'b=2&c=3',
    dataType: 'json',
    success: function(data) {
        console.debug('success');
        $('#result').text(data.message);
    }
})
</script>
</body>
</html>

Below is a capture of Chrome developer tools:


2022-09-22 14:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.