2013-10-24 44 views
0

我不能爲我的生活弄清楚爲什麼這個ajax調用會一直返回一個錯誤。 (我是新來的網絡世界,所以我猜我錯過了簡單的東西在這裏:S)Ajax調用總是從mvc動作返回錯誤,即使它工作正常

在我的cshtml我有一個Select2多值選擇框持有標籤。

$(function() { 
     $('#tagSelector').select2({ 
      placeholder: 'Select a tag...', 
      multiple: true, 
      ajax: { 
       url: '@Url.Action("SearchTags", "UnitDetails")', 
       dataType: 'json', 
       data: function (term, page) { 
        return { 
         searchTerm: term 
        }; 
       }, 
       results: function (data, page) { 
        return { results: data }; 
       } 
      }, 
      createSearchChoice: function (term) { 
       return {id: term, text: term}; 
      } 
     }).on("removed", function(e) { 
      var url = '@Url.Content("~/UnitDetails/UnTagUnit/" + Model.ViewUnitContract.Id)'; 
      var id = e.val; 
      var tagName = e.choice.text; 
      console.log(id + " : " + tagName); 

      $.ajax({ 
       url: url, 
       data: { selectedItem: tagName }, 
       type: 'GET', 
       dataType: 'json', 
       success: function() { 
        toastr.options = { 
         "closeButton": true, 
         "debug": false, 
         "positionClass": "toast-top-right", 
         "onclick": null, 
         "showDuration": "300", 
         "hideDuration": "1000", 
         "timeOut": "3000", 
         "extendedTimeOut": "1000", 
         "showEasing": "swing", 
         "hideEasing": "linear", 
         "showMethod": "fadeIn", 
         "hideMethod": "fadeOut" 
        }; 
        toastr.success("Tag deleted from unit.", "Success!"); 
       }, 
       error: function() { 
        toastr.options = { 
         "closeButton": true, 
         "debug": false, 
         "positionClass": "toast-top-right", 
         "onclick": null, 
         "showDuration": "300", 
         "hideDuration": "1000", 
         "timeOut": "3000", 
         "extendedTimeOut": "1000", 
         "showEasing": "swing", 
         "hideEasing": "linear", 
         "showMethod": "fadeIn", 
         "hideMethod": "fadeOut" 
        }; 
        toastr.error("Could not delete tag from unit.", "Oops!"); 
       } 
      }); 
     }); 

在我的控制器中,UnTagUnit(int id, string selectedItem)看起來是這樣的:

然後,當我嘗試到我的控制器做一個AJAX調用中刪除標籤,它,即使我成功地完成操作返回一個錯誤
[HttpGet] 
public JsonResult UnTagUnit(int id, string selectedItem) 
{ 
    try 
    { 
     UnitClient.UnTagUnit(id, selectedItem); 

     // what to return if success? 
    } 
    catch (Exception e) 
    { 
     // Log and handle 
     // what to return if error? 
    } 
} 

如前所述,該方法正常工作,我的意思是完成UnTagUnit(id, selectedItem)部分成功(wcf服務)。

所以,我想我要麼錯過了某些東西,要麼做了根本性的錯誤。還嘗試了不同的方法sucs返回new Json(new { success = true})

回答

3

需要,因爲你正在使用GET方法在你的行動,使用return JsonRequestBehavior.AllowGet

[HttpGet] 
public JsonResult UnTagUnit(int id, string selectedItem) 
{ 
    try 
    { 
     UnitClient.UnTagUnit(id, selectedItem); 

     // what to return if success? 
     return Json(data, JsonRequestBehavior.AllowGet) 
    } 
    catch (Exception e) 
    { 
     // Log and handle 
     // what to return if error? 
     //you can throw error here .. 
     throw ("Un tag failed "+e.Message); 
     ///or return it as a message 
     return Json(new { success = false,message="Un tag failed "+e.Message} , JsonRequestBehavior.AllowGet) 
    } 
} 
+0

謝謝你,這麼多! :) –

相關問題