jquery.ajax type:post strange behavior

822 Views Asked by At

update start

oooooops, I've just found that it's not a get & post problem, but a SpringMVC RequestMapping & trailing slash & http 302 problem.

when a /editor get-request is called, 302 returns, and response location is /editor/,

/editor network

response detail

but if I change @RequestMapping("editor") of EditorController to @RequestMapping("anyWords"), then everything works fine.

enter image description here

So the question is why @RequestMapping("editor") has the trailing slash problem while @RequestMapping("anyWords") works fine.

update end

I'm just trying to call a simple ajax-post request, but getting confused by the strange behavior.

I claim the ajax type : "post", but if I don't give the url a '/' suffix like "/editor" instead of "/editor/", the result will always be a "get" request.

Is it a must to add a '/' suffix to a 'post' request?

here is some conf and code

SpringMVC Controller

@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";
    }
}

js function to call ajax request

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 plugin

<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>

I don't have 10 reputations to post the network image... :( when I call the js function, it firstly call a post request that has a code302 status, and then call a get request that has a code200 status, like the following text

name    method  status  type        initiator
editor  POST    302     Pending     jquery-2.0.3.js:7845
editor/ GET     200     text/html   http://localhost:8080/editor

I've just create another test controller and everything works fine. I mean without a '/' suffix, an ajax-post request can still be called.

anyone has a sense of the difference between EditorController and 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;
    }
}
0

There are 0 best solutions below