简体   繁体   English

即使操作成功,Ajax post 也返回 404

[英]Ajax post returning 404 even when the operation is successful

I am making an ajax post call to my backing spring controller.我正在对我的支持弹簧控制器进行 ajax post 调用。 I see that the data is saved successfully in the db as a result of post but on the browser console I see that POST throws 404. Also, once the call returns from the spring controller, error function of ajax call is envoked.我看到数据作为 post 的结果成功保存在数据库中,但在浏览器控制台上我看到 POST 抛出 404。此外,一旦调用从 spring 控制器返回,ajax 调用的错误函数就会被调用。

Can someone please tell me what I am missing here.有人可以告诉我我在这里缺少什么。

$('#submitMessage').submit(function(e){
    e.preventDefault();
    var formData = {};
    var msg=document.getElementById('iconLeft4-1');
    var url = "${context}/wasadmin/support/ajax/send";
    formData['comment']=msg.value;
    formData['commented_by']='1';
    formData['supportId']='1';
    formData['userType']='O';
    console.log(JSON.stringify(formData));
    $.ajax({
        type : 'POST',
        contentType: "application/json",
        url : url,
        dataType : 'json',
        data:JSON.stringify(formData),
        success:function(data,status,xhr){
            console.log('saved successfully');

            },
        error:function(data,status,xhr){
            console.log('error occured');  // This gets  printed
            }

        });

Controller控制器

@PostMapping(value="/ajax/send")
    public void sendSupportMessage(@RequestBody SupportConversationDTO supportConversationDTO) {
        supportConversationService.save(supportConversationDTO);
        return;
    }

浏览器控制台错误

In your ajax request you are using dataType:'json' , jQuery's Ajax-Related Methods Description Says在您的 ajax 请求中,您使用的是dataType:'json'jQuery 的 Ajax 相关方法描述说

// The type of data we expect back
    dataType : "json",

But from your controller, you are returning void!!!但是从您的控制器来看,您返回无效!!!

You need to update your ajax request, remove dataType and update success:function您需要更新您的 ajax 请求,删除dataType并更新success:function

$.ajax({
    type : 'POST',
    contentType: "application/json",
    url : url,
    data:JSON.stringify(formData),
    success:function(data) {
        console.log('saved successfully');
    },
    error:function(data,status,xhr) {
        console.log('error occured');
    }
});

Then change your controller code, return something from it and add @ResponseBody然后更改您的控制器代码,从中返回一些内容并添加@ResponseBody

@PostMapping(value="/ajax/send")
@ResponseBody
public String sendSupportMessage(@RequestBody SupportConversationDTO supportConversationDTO) {
    supportConversationService.save(supportConversationDTO);
    return "success";
}

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

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