【问题标题】:Ajax call is not returning to success from controllerAjax 调用未从控制器返回成功
【发布时间】:2017-04-05 22:22:09
【问题描述】:

在下面的代码中,我正在执行 ajax 调用并调用控制器“/反馈”,并且从控制器中,我返回一个字符串值作为“Y”。但是每次,它都会将我重定向到错误 Jsp。

任何帮助将不胜感激。

Ajax 调用:

document.getElementById("modal_feedback").addEventListener("submit", function(e) {
        var form = this;
         var name = form.name.value;
         var rating = form.overall.value;
         var msg = form.message.value;
         if(name == "") {
           alert("Please enter your Name");
         form.name.focus();
         e.preventDefault();
         } else if(rating == "") {
            alert("Please select a rating");
         form.overall[0].focus();
         e.preventDefault();
          } else if(msg == "") {
             alert("Please enter your comment in the Message box");
          form.message.focus();
         e.preventDefault();
       }
       $.ajax({
          type: "POST",
          url: "feedbackData.htm?ratingId="+rating+"&msg="+msg,
          success: function(response) {
         console.debug(response);
         if(response == 'Y'){
         $('#contact_form').html("<div id='message'></div>");
         $('#message').html("<h2>Contact Form Submitted!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(1500, function() {
            $('#message').append("<img id='checkmark' src='images/icon_pdf.png' />");
          });
    }
    }
  });
  return false;

}, false);

控制器代码:

@RequestMapping(value = "/feedbackData")
    public @ResponseBody String getFeedbackData(String ratingId, String msg) throws UnsupportedEncodingException{
        System.out.println("Inside FeedbackController..");
        try{
            feedbackService.updateFeedback(ratingId,msg);
            return "Y";
        }catch(Exception e)
        {
            logger.error("Exception in Login :" + e);
            return "N";
        }
    }
}

【问题讨论】:

  • 你在 url 中使用 POST 方法和发送属性,这是有问题的,为什么你使用 feedbackdata.html 而不是 feedbackdata。

标签: javascript jquery ajax spring-mvc


【解决方案1】:

我已经尝试过数据类型:“html”,它开始返回响应而不是 error.jsp。更新JS代码如下

      document.getElementById("modal_feedback").addEventListener("submit", function(e) {
      e.preventDefault();   
      var form = this;
      var name = form.name.value;
      var rating = form.overall.value;
      var msg = form.message.value;
          if(name == "") {
             alert("Please enter your Name");
             form.name.focus();
             e.preventDefault();
          } else if(rating == "") {
      alert("Please select a rating");
      form.overall[0].focus();
      e.preventDefault();
      } else if(msg == "") {
         alert("Please enter your comment in the Message box");
         form.message.focus();
         e.preventDefault();
     }
     $.ajax({
    type: "POST",
    url: "feedbackData.htm?ratingId="+rating+"&msg="+msg,
    async : false,
    dataType: "html",
    success: function(response) {
        console.debug(response);
        if(response == 'Y'){
        $('#modal_window').html("<div id='message'></div>");
        $('#message').html("<h2>Feedback Form Submitted!</h2>").append("<p>We will be in touch soon.</p>")
        }
    },
    error : function(e) {
        alert('Error: ' + e);
    }
  });
   return false;
});

【讨论】:

    【解决方案2】:

    尝试通过添加 dataType : "html" 来更新您的 ajax 代码,以便它接受如下字符串形式的响应:

    $.ajax({
        type: "GET",
        url: "feedbackData.htm?ratingId="+rating+"&msg="+msg,
        dataType: "html",
        success: function(response) {
            console.debug(response);
            if(response == 'Y'){
                $('#contact_form').html("<div id='message'></div>");
                $('#message').html("<h2>Contact Form Submitted!</h2>")
                .append("<p>We will be in touch soon.</p>")
                .hide()
                .fadeIn(1500, function() {
                    $('#message').append("<img id='checkmark' src='images/icon_pdf.png' />");
                });
            }
        }
    });
    

    另请阅读jquery ajax官方文档以获取更多说明here

    【讨论】:

    • 您可以将此答案标记为有用,如果它对您有用,以便它帮助面临相同问题的其他人。谢谢
    • 我尝试了您的建议,但没有奏效。我仍然面临同样的问题。
    • success 函数下尝试console.log(response) 并在此处发送您的回复。
    • 在控制台上给我以下错误。未捕获的 ReferenceError:在 HTMLFormElement. (localhost:8080/TDP/resources/js/feedback-modal.js:36:4) 的 eval 处未定义响应(eval at evaluate (:85:21), :1:1)
    • 你确定吗?您已经先将dataType: "html", 添加到您的ajax 函数中了吗?
    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多