2017-04-05 49 views
0

以下のコードでは、ajax呼び出しを行い、コントローラ '/ feedback'を呼び出しています。コントローラから、文字列値を "Y"として返します。しかし毎回、エラーJspにリダイレクトされています。コントローラからAjax呼び出しが成功しない

ご協力いただければ幸いです。

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"; 
     } 
    } 
} 
+0

あなたはPOSTメソッドを使用してURLに属性を送信していますが、これは間違っています。なぜfeedbackdataの代わりにfeedbackdata.htmlを使用するのですか? – mustafa918

答えて

0

それは以下のような文字列として応答を受け入れるよう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

+0

あなたが同じ問題に直面している他の人に役立つように、この回答を役に立つとマークすることができます。ありがとう –

+0

私はあなたの提案を試みたが、うまくいかなかった。私はまだ同じ問題に直面しています。 – MSV

+0

'success'関数の' console.log(response) 'を試して、ここにあなたの応答を送ってください。 –

1

私はデータ型を試しました: "html"は応答を返し始め、error.jspは受け取りません。以下のようにJSコードを更新しました。

関連する問題