2016-01-22 67 views
3

我工作的jQuery AJAX調用,但我想檢查請求的結果,所以我說jQuery的努力來處理Ajax故障和sucess不工作

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

但這不是工作,使我的整體功能停止even.so在這裏工作是整個功能,可有人告訴我,我是什麼做wrong`

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"); 
    } 
}`    

當我刪除它的作品,因爲我預期它to.And如果它的事項使用Grails

很成功
+2

你錯過了'''的'}在'success'塊 –

回答

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"); 
    } 
}`    

這將解決這個問題

+0

此停止我的功能後,從下降,但仍然無法獲得成功或失敗的消息。 – robel

+1

如果你需要一個json輸出,用'dataType:「發送你的ajax請求:」json「' – Thanga