javascript - Ajax 向 Controller 发出调用后收到 400(错误请求)

标签 javascript jquery ajax

我正在尝试使用 jquery 实现 ajax 调用。当我提交调用时,它抛出 400 Bad Request..不确定我在 ajax 调用中哪里做错了。.需要帮助来修复此问题..

 submitHandler:function(form){
            var emailSub = $("#emailSubTxtArea").val();
            var emailBody = $("#emailBodyTxtArea").val();
            if(confirm("You are about to send Email Communication, Are you sure..?")){
            $.ajax({
                type: "POST",
                url:  $("#applicationUrl").val() +"/web/utilities/sendEmailMessage",
                dataType: "json",
                //cache:false,
                contentType: "application/json; charset=utf-8",
                data:JSON.stringify({emailSubject : emailSub,emailMsg : emailBody}), 
                success:function(data)
                {
                    console.log("Sending Email Notification was success.");
                },
                error: function(x, t, m) {
                    console.trace();
                    if (!(console == 'undefined')) {
                    console.log("ERROR: " + x + t
                            + m);
                    }
                    }
           });
        }
return false;
        }

我的 Controller 代码:

@RequestMapping(value="/sendEmailMessage",method=RequestMethod.POST)
    public ModelAndView sendEmailCommunication(@RequestParam("emailSubject") String emailSubject,@RequestParam("emailMsg") String emailBody,HttpServletRequest request){
        ModelAndView view = null;
        StringBuffer sMsg = new StringBuffer();
        StringBuffer eMsg = new StringBuffer();
        boolean isAdmin = false;
        try{
        String loggedInUser = request.getHeader("sm_user").trim();
         isAdmin = getUserAdminRights(request);
        if(isAdmin){
            boolean status = emailService.sendEmailCommuncation(emailSubject,emailBody);
            if(status){
                sMsg.append(" Sending SiteMinder Notification Email was Success.");
            }
            else{
                eMsg.append(" Oops! Something went wrong while sending Email Notification. Pls check logs");
            }
        }
        else{
             view = new ModelAndView("redirect:/web/utilities/not_authorized");
             return view;
        }
        }
        catch(Exception ex){
            ex.printStackTrace();
            eMsg.append("Oops! Something went wrong while sending Email Notification. Pls check logs");
        }
        view = new ModelAndView("EmailCommunication");
        view.addObject("isAdmin", isAdmin);
        view.addObject("sMsg", sMsg.toString());
        view.addObject("eMsg", eMsg.toString());
        return view;
    }

过去 4 小时我真的很头疼……需要帮助。 谢谢..

最佳答案

您的数据键需要引号。将 emailSubject: emailSub,emailMsg : emailBody 更改为 "emailSubject": emailSub, "emailMsg": emailBody

您在 submitHandler 的最后还缺少一个结束 },可能只是粘贴错误?

关于javascript - Ajax 向 Controller 发出调用后收到 400(错误请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38018526/

相关文章:

javascript - 产生多个 Promise

javascript - 我怎样才能让它更快。通过从数组中删除一个元素,检查数组是否在增加序列

javascript - 为所选链接的所有级别的父级添加不同的类

javascript - Datatables.net 渲染和应用分页速度慢

javascript - append() 不是 ajax 数据字符串中的函数 jQuery

javascript - 动态添加 URL 到选择框

javascript - 窗口焦点事件 ctrlkey 未定义

javascript - 表单验证在 jQuery 中不起作用

php - 为 jQuery/Ajax 从 URL(如 php 的 $_GET)获取数据?

python - web2py Ajax 搜索