jQuery试图处理ajax失败和成功无法正常工作

时间:2016-01-22 12:02:39

标签: javascript jquery ajax grails

我有一个正在运行的jQuery ajax调用,但我想检查请求的结果,所以我添加了

success: function(data) {
    if(data.status == 'success'){
        alert("Thank you for subscribing!");
    }else if(data.status == 'error'){
        alert("Error on query!");
    }
}

但这不起作用,使我的整个功能停止工作。所以这里是整个功能,有人可以告诉我做错了什么。

   function addGoal()  {

    var description = $('.description').val();
    var measure = $('.measure').val();

    if(  description.trim()  &&  measure.trim()  ){
        if(window.confirm("Are you sure? You can't edit or remove goal(s) you have added here.")==true) {

            $.ajax({
                url: '${g.createLink( controller:'review', action:'saveGoal', params:[id: params.id]  )}',
                data: {
                    "description": description,
                    "measure": measure
                },

               success: function(data) {
                    if(data.status == 'success'){
                        alert("Thank you for subscribing!");
                    }else if(data.status == 'error'){
                        alert("Error on query!");
                    }
                }
                type: 'POST'                 
            });
        }
    }else {
        showErrorMessage("Description and Measure fields cant be empty");
    }
}`             

当我取消成功时,它按照我的预期运作。如果重要的是使用grails

1 个答案:

答案 0 :(得分:3)

语法错误,缺少逗号

   function addGoal()  {

    var description = $('.description').val();
    var measure = $('.measure').val();

    if(  description.trim()  &&  measure.trim()  ){
        if(window.confirm("Are you sure? You can't edit or remove goal(s) you have added here.")==true) {

            $.ajax({
                url: '${g.createLink( controller:'review', dataType: "json", action:'saveGoal', params:[id: params.id]  )}',
                data: {
                    "description": description,
                    "measure": measure
                },

               success: function(data) {
                    if(data.status == 'success'){
                        alert("Thank you for subscribing!");
                    }else if(data.status == 'error'){
                        alert("Error on query!");
                    }
                },
                type: 'POST'                 
            });
        }
    }else {
        showErrorMessage("Description and Measure fields cant be empty");
    }
}`            

这将解决问题