2013-03-07 62 views
1

我有一些調用重新填充下拉列表的函數的JavaScript。有2個調用填充2個不同的下拉列表。這兩個工作在當地開發很好。在服務器上只有一個工程。一個錯誤了。我遠程調試,並調用到達函數和函數返回正確的結果。它在離開發生錯誤的功能之後。應用程序是asp.net mvc 3,服務器是windows server 2008 iis7。如何解決ajax返回失敗的問題

如何縮小導致問題的原因。

<script type="text/javascript"> 


function getSects(abbr) { 
    $.ajax({ 
     url: "@Url.Action("SectionSwitch", "Assets")", 
     data: { abbreviation: abbr }, 
     dataType: "json", 
     type: "POST", 
     error: function() { 
      alert("An error occurred."); 
     }, 
     success: function (data) { 
      // var test = JSON.parse(data); 
      //alert(test); 
      var items = ""; 
      $.each(data, function (i, item) { 
       items += "<option value=\"" + item.sectionNum + "\">" +  item.sectionname + "</option>"; 

      }); 

      $("#Asset_Section_SectionKey").html(items); 
     } 
    }); 
} 

function getDivs(abbr) { 
    $.ajax({ 
     url: "@Url.Action("DivisionSwitch", "Assets")", 
     data: {abbreviation: abbr}, 
     dataType: "json", 
     type: "POST", 
     error: function() { 
      alert("An error occurred."); 
     }, 
     success: function (data2) { 
      // var test = JSON.parse(data); 
      //alert(test); 
      var items = ""; 
      $.each(data2, function(i, item) { 
       items += "<option value=\"" + item.DivisionKey + "\">" + item.DivisionDescription + "</option>"; 

      }); 

      $("#Asset_Section_Division_DivisionKey").html(items); 
     } 
    }); 
} 

$(document).ready(function(){ 
    $("#Asset_Section_Division_Department_DepartmentKey").change(function() { 
     var abbr = $("#Asset_Section_Division_Department_DepartmentKey").val(); 

     getDivs(abbr); 
    }); 

    $("#Asset_Section_Division_DivisionKey").change(function() { 
     var abbr = $("#Asset_Section_Division_DivisionKey").val(); 

     getSects(abbr); 
    }); 
}); 
</script> 

它的函數getDivs拋出錯誤。下面是功能:

public ActionResult DivisionSwitch(int abbreviation) 
    { 

     var newdivision = from f in db.Divisions 
          where f.DepartmentKey == abbreviation 
          select f; 

     return Json(newdivision); 

    } 

    public ActionResult SectionSwitch(int abbreviation) 
    { 

     var newsection = (from t in db.Sections 
          where t.DivisionKey == abbreviation 
          select new sectionInfo { sectionNum = t.SectionKey, sectionname = t.SectionDesciption }); 

     return Json(newsection); 

    } 

回答

1

你的AJAX方法改變你的錯誤性質採取3個參數(jqXHR, textStatus, errorThrown)並提醒errorThrowntextStatus

error: function (jqXHR, textStatus, errorThrown) { 
     alert("Error, status: " + textStatus + " error: " + errorThrown); 
    }, 
+0

謝謝,我得到內部服務器錯誤。現在我需要弄清楚這意味着什麼。 – mdarling 2013-03-07 15:12:54

+0

@mdarling嗯,這可能表明該URL是錯誤的。在錯誤函數中,還會提示'「@ Url.Action(」SectionSwitch「,」Assets「)」'並確保URL正確。 – mattytommo 2013-03-07 15:41:32

+0

它不是一個URL的問題。我運行遠程調試,可以看到該功能被觸發。它在返回之後發生問題。雖然謝謝! – mdarling 2013-03-07 15:51:28

1

搭建error handler,看看它有什麼要說

$(document).ajaxError(function(event, jqxhr, settings, exception) { 
    console.log(jqxhr); 
    console.log(exception); 
}); 
+0

感謝您的回覆,我使用了其他答案之一,並被告知這是一個內部服務器錯誤。你的解決方案會給我更多的細節嗎? – mdarling 2013-03-07 15:15:42

+0

用像fiddler這樣的工具查看響應,看看它是否會給你更多的信息。聽起來你應該改變與MVC3框架的服務器上的錯誤處理。 – epascarello 2013-03-07 15:19:23

+0

謝謝,我剛剛安裝了小提琴手。我會看看。一個電話工作而另一個不工作似乎很奇怪。 – mdarling 2013-03-07 15:27:30

1

您$阿賈克斯的錯誤部分可以接受jqXHR類型的三個參數,textStatuserrorThrown

第一個參數是jqXHR類型的對象,另外兩個是字符串。您可以使用jqXHR.responseText來查看錯誤。

$.ajax({ // Some settings 
    error: function(jqXHR, textStatus, errorThrown) { 
     alert(jqXHR.responseText); 
     // or 
     console.log(jqXHR); 
    } 

據jQuery的網站,他們的榜樣建議如下:

$.ajax({ // your settings 

}).done(function(data, textStatus, jqXHR) { 
    // Handle success 
}).fail(function(jqXHR, textStatus, errorThrown) { 
    // Handle error here 
}); 

欲瞭解更多信息,看看jQuery.ajax()和閱讀折舊部分。

+0

謝謝,真的很快三個響應,我在另一個評論中提到它只是告訴我它的內部服務器錯誤。 – mdarling 2013-03-07 15:14:35

+1

我想通了。謝謝你的幫助 – mdarling 2013-03-07 16:04:48

0

我欣賞所有的幫助,但我決定重寫什麼是返回類似的工作電話正在返回。顯然,服務器遇到了返回問題。

public ActionResult DivisionSwitch(int abbreviation) 
{ 

    var newdivision = from f in db.Divisions 
         where f.DepartmentKey == abbreviation 
         select f; 

    return Json(newdivision); 

} 

public ActionResult SectionSwitch(int abbreviation) 
{ 

    var newsection = (from t in db.Sections 
         where t.DivisionKey == abbreviation 
         select new sectionInfo { sectionNum = t.SectionKey, sectionname = t.SectionDesciption }); 

    return Json(newsection); 

} 

現在我已經改變了代碼這樣:

public ActionResult DivisionSwitch(int abbreviation) 
    { 

     var newdivision = (from f in db.Divisions 
          where f.DepartmentKey == abbreviation 
          select new DivisionInfo { DivisionNum = f.DivisionKey, Divisionname = f.DivisionDescription }); 

     return Json(newdivision); 
} 

我剛剛創建了一個新的類來存儲結果,並縮小它需要兩個精密組件。

 public class DivisionInfo 
{ 
    public int DivisionNum { get; set; } 
    public string Divisionname { get; set; } 
} 
相關問題