简体   繁体   English

Ajax发布请求调用不起作用

[英]Ajax post request call doesn't work

I have a problem with the my Ajax call. 我的Ajax通话有问题。 Here is my controller: 这是我的控制器:

@RequestMapping(value = { "/sendMsg" }, method = RequestMethod.POST)
public ModelAndView postSendMessage(@ModelAttribute("message") MsgParam param) {
ModelAndView result= new ModelAndView("sendMessage");
...Controller logic...
}

I have a form in my sendMessage.jsp that should return json. 我的sendMessage.jsp中有一个表单,该表单应返回json。 However the ajax call wouldn't work so now I am trying to get this to return html through ajax call, which should make no difference to the user. 但是ajax调用无法正常工作,因此现在我试图通过ajax调用返回html,这对用户没有影响。 All I want to do for now is make a simple ajax call with html return. 我现在要做的就是用html return做一个简单的ajax调用。 And here is the form in my jsp: 这是我的jsp中的表格:

<form:form id="postForm" method="POST" commandName="message" action="${pageContext.request.contextPath}/sendMsg">
    ... input fields with path attributes...
</form:form>

This works as expected, so that is why omitted the logic. 这可以按预期工作,所以这就是为什么省略逻辑的原因。 I tried adding the following script after the form: 我尝试在表单后添加以下脚本:

<script>
        $('#postForm').submit(function(evt) {
                evt.preventDefault();
                alert("ajax");
                msgData = $('#postForm').serialize();
                $.ajax({
                url: $('#postForm').action,
                type: 'POST',
                data: msgData
                });
        });
    </script>

So now when I try submitting the form same as before, I get the ajax alert before the ajax call popping up but the page post request is not sent and nothing happens. 因此,现在当我尝试提交与以前相同的表单时,我会在弹出ajax调用之前收到ajax警报,但是页面发送请求未发送且没有任何反应。 Does anyone have any idea what is wrong? 有人知道哪里出了问题吗?

Thanks a lot.. 非常感谢..

Try this to inspect what you are sending and receiving: 尝试使用以下方法检查您要发送和接收的内容:

<script type="text/javascript">
//Wait for jQuery to be loaded
$(function(){

function _submitHandler(evt){
            evt.preventDefault();
            //form data placeholder
            var formData={};
            //Serialize the form as an array of objects{name and value}
            var msgData = $('#postForm').serializeArray();
            //fill the formData object with name:value pairs
            //from the serialized array
            for(var i=0, l=msgData.lenght; i<l; i++){
              formData[msgData[i].name]=msgData[i].value;
            }
            //Prepare ajax Object
            var ajaxObj = {
                url: $('#postForm')[0].action,
                type: 'POST',
                data: formData
            };
            //Inspect ajax object in console
            console.log("Ajax Call");
            console.log(ajaxObj);
            $.ajax(ajaxObj,function(response){
                console.log("Server Response");
                console.log(response);});
            }
$('#postForm').submit(_submitHandler);

});
</script>

For the URL you need to subindex the jQuery url: $('#postForm')[0].action or get the attribute url: $('#postForm').attr('action'); 对于URL,您需要为jQuery url子索引:$('#postForm')[0] .action或获取属性url:$('#postForm')。attr('action'); obviously the second one will be only the path and the first one will give you the whole URL. 显然,第二个只是路径,第一个将为您提供整个URL。 The for loop can be skipped if you do this: 如果执行此操作,则可以跳过for循环:

$("#postForm").serializeArray().forEach(function(el){formData[el.name]=el.value});

the problem with the forEach is that is not supported by all the browsers** forEach的问题是并非所有浏览器都支持**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM