更新開始
おっと、get & post の問題ではなく、SpringMVC RequestMapping & 末尾のスラッシュ & http 302 の問題であることがわかりました。
/editor get-request が呼び出されて戻り302
、応答の場所が/editor/の場合、
しかし、 を に変更@RequestMapping("editor")
するEditorController
と@RequestMapping("anyWords")
、すべて正常に動作します。
したがって、問題は、正常に動作@RequestMapping("editor")
しているときに末尾のスラッシュの問題が発生する理由@RequestMapping("anyWords")
です。
更新終了
単純な ajax-post リクエストを呼び出そうとしていますが、奇妙な動作に混乱しています。
私は ajax タイプを「post」と主張していますが、URL に「/editor/」ではなく「/editor」のような「/」サフィックスを付けないと、結果は常に「get」リクエストになります。
「投稿」リクエストに「/」サフィックスを追加する必要がありますか?
ここにいくつかの設定とコードがあります
SpringMVC コントローラー
@Controller
@RequestMapping("/editor")
public class EditorController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView getEditor() {
return new ModelAndView("/WEB-INF/editor.jsp");
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String saveEditor(Article article) {
// some code
return "something";
}
}
ajaxリクエストを呼び出すjs関数
MyShare.editor = {
checkAndSubmit : function(){
var title = $("#title").val();
var author = $("#author").val();
$.ajax({
url : "/editor/",
// problem here, without a '/' suffix,
// it will always call a get request.
// url : "/editor"
type : "POST",
data : {
'title' : title,
'author' : author
},
success : function(response){
// some code
},
error : function(response){
// some code
}
});
}
};
tomcat プラグイン
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<configuration>
<url>http://localhost:8080/manager/html</url>
<server>tomcat-local</server>
<path>/</path>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
</plugins>
ネットワーク イメージを投稿するための 10 のレピュテーションがありません... :( js 関数を呼び出すと、最初に code302 ステータスを持つポスト リクエストが呼び出され、次に code200 ステータスを持つ get リクエストが呼び出されます。次のテキスト
name method status type initiator
editor POST 302 Pending jquery-2.0.3.js:7845
editor/ GET 200 text/html http://localhost:8080/editor
別のテスト コントローラーを作成したところ、すべて正常に動作します。つまり、「/」サフィックスがなくても、ajax-post リクエストを呼び出すことができます。
誰もが EditorController と TestController の違いを感じています
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String get(String title) {
return "get : " + title;
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String post(String title) {
return "post" + title;
}
}