2009-09-22 8 views
2

로드시 jquery 스크립트를 실행하는 ASP.net mvc 페이지가 있습니다. 스크립트는 컨트롤러에서 작업을 호출하고 드롭 다운 목록을 수화합니다.페이지로드시 jquery 스크립트가 실행되지 않습니다.

이것은 내 dev 컴퓨터에서 작동하지만 웹 서버 (IIS 6을 실행하는 Win 2k3 상자)에 배포하면 페이지가로드되지만 스크립트가 실행되어 빈 드롭 다운 목록이 표시됩니다.

나는 scripts 폴더에 jquery-1.3.2.js 파일이 있고 웹 서버에 aspnet_isapi.dll에 매핑을 추가했습니다. 내가 놓친 다른 것이 있습니까?

이것은 내 컴퓨터에서 작동하지만 웹 서버에서 작동하는 드롭 다운 목록을 보완하는 페이지의 일부입니다. 스크립트가 ApplicationSettings 컨트롤러를 호출하여 해당 JSON 객체를 해독하는 JSON 객체를 얻을 수 있습니다. 당신은 당신의 코드를 삽입 다운 목록

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server"> 
    <script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     // Wait for the document to be ready 
     $(document).ready(function() 
     { 
      var selectedApp = $('#selectedApplication').val(); 
      var selectedMac = $('#selectedMachine').val(); 

      // Get the list of applications and populate the applications drop down list 
      $.getJSON("/ApplicationSettings/Applications/List", function(data) 
      { 
       var items = "<option>----------- Select Application to Configure ----------</option>"; 
       $.each(data, function(i, application) 
       { 
        var selected = (application.Value == selectedApp) ? 'selected' : ''; 
        items += "<option value='" + application.Value + "'" + selected + ">" + application.Text + "</option>"; 
       }); 
       $("#Applications").html(items); 
      }); 

      // Get the list of machines where the selected application is installed and populate the machines drop down list 
      $("#Applications").change(function() 
      { 
       if ($("#Applications").attr("value") != "") 
       { 
        // Enable the Machines DDL if a valid application is selected 
        $("#Machines").removeAttr("disabled"); 

        // Populate the machines DDL with a list of machines where the selected application is installed 
        $.getJSON("/ApplicationSettings/Machines/List/" + $("#Applications > option:selected").attr("value"), function(data) 
        { 
         // Set the first item in the list 
         var items = "<option>---------- Select Machine -----------</option>"; 

         // Retrieve the list of machines for th selected application from the database 
         $.each(data, function(i, machine) 
         { 
          var selected = (machine.Value == selectedMac) ? 'selected' : ''; 
          items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>"; 
         }); 

         // Add the items retrieved to the Machines DDL 
         $("#Machines").html(items); 

         if ($("#Machines").attr("value") != "") 
         { 
          $("#btnSearch").removeAttr("disabled"); 
         } 
         else 
         { 
          $("#btnSearch").attr("disabled", "disabled"); 
         } 
        }); 
       } 
       else 
       { 
        // If a valid application has not been selected then disable the Machines DDL 
        $("#Machines").attr("disabled", "disabled"); 
        $("#btnSearch").attr("disabled", "disabled"); 
       } 
      }); 

      if (selectedApp != "") 
      { 
       $("#Machines").removeAttr("disabled"); 

       $.getJSON("/ApplicationSettings/Machines/List/" + selectedApp, function(data) 
       { 
        var items = "<option>---------- Select Machine -----------</option>"; 
        $.each(data, function(i, machine) 
        { 
         var selected = (machine.Value == selectedMac) ? 'selected' : ''; 
         items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>"; 
        }); 
        $("#Machines").html(items); 
       }); 

       if (selectedMac != "") 
       { 
        $("#btnSearch").removeAttr("disabled"); 
       } 
       else 
       { 
        $("#btnSearch").attr("disabled", "disabled"); 
       } 
      } 
      else 
      { 
       $("#Machines").attr("disabled", "disabled"); 
       $("#btnSearch").attr("disabled", "disabled"); 
      } 
     }); 


     function saveSelectedApplication() 
     { 
      $("#selectedApplication").val(""); 
      $("#selectedMachine").val(""); 
      $("#selectedApplication").val($("#Applications").attr("value")); 
      if ($("#Applications").attr("value") != "") 
      { 
       $("#Machines").removeAttr("disabled"); 
       if ($("#Machines").attr("value") != "") 
       { 
        $("#btnSearch").removeAttr("disabled"); 
       } 
       else 
       { 
        $("#btnSearch").attr("disabled", "disabled"); 
       } 
      } 
      else 
      { 
       $("#Machines").attr("disabled", "disabled"); 
       $("#btnSearch").attr("disabled", "disabled"); 
      } 
     } 

     function saveSelectedMachine() 
     { 
      $("#selectedMachine").val(""); 
      $("#selectedMachine").val($("#Machines").attr("value")); 
      if ($("#Machines").attr("value") != "") 
      { 
       $("#btnSearch").removeAttr("disabled"); 
      } 
      else 
      { 
       $("#btnSearch").attr("disabled", "disabled"); 
      } 
     } 
    </script> 
+0

스크립트가 실행되고 있습니까? 스크립트 맨 위에서 간단한 경고? – littlechris

+0

global.asax 파일에서 맞춤 라우팅을 사용합니까? 아래에 남긴 주석은 아래에 구현할 때까지 내가 가진 오류와 같습니다. 나는 JS가 내 스크립트 경로에 문제가있어 너무 일찍 돌아가고 있다고 생각한다. 내가 \t \t \t $ .getJSON ("../ ApplicationSettings로 변경하는 경우 – littlechris

+0

스크립트는 줄 $ .getJSON ("/ ApplicationSettings/응용 프로그램/목록 ", 기능 (데이터) 를 작업의 위치에 문제가 있었다/응용 프로그램/목록 ", 함수 (데이터) 그것은 작동합니까 내가 정말로해야 할 일은 올바르게 행동 경로를 해결할 수있을 것 같아요. jquery에서 이것을 수행하는 방법을 모르십니까? 아이디어가 있습니까? – user99513

답변

5

스크립트 경로 지정에 문제가 있습니다. 이 확장 메서드를 사용하여 정렬했습니다. 같은 모든 스크립트를 등록 jsut 다음

<%@ Import Namespace="MYNAMESPACE.Helpers" %> 

과 :

public static class HtmlHelperExtensions 
    { 
     /// <summary> 
     /// Scripts the specified HTML to allow for correct pathing of the resource. 
     /// </summary> 
     /// <param name="html">The HTML.</param> 
     /// <param name="path">The path.</param> 
     /// <returns></returns> 
     public static string Script(this HtmlHelper html, string path) 
     { 
      var filePath = VirtualPathUtility.ToAbsolute(path); 
      return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>"; 
     } 
    } 

그런 다음 마스터 페이지에 넣고

<%=Html.Script("~/Scripts/jquery-1.3.2.min.js")%> 

뿐만 아니라 때라도 도우미를 위치 : implemeting보십시오 :

public static string AbsolutePath(this HtmlHelper html, string path) 
{ 
    return VirtualPathUtility.ToAbsolute(path); 
} 

그리고 c를 변경하십시오. 모두에서

$.getJSON("<%=Html.AbsolutePath("~/ApplicationSettings/Machines/List/")%>" 

MVC ViewEngine에서 절대 경로를 삽입해야합니다.

+0

한 좋은 간단한 예.이 작품 littlechris – CmdrTallen

+0

은!. 및 솔루션을 설명하는 당신에게 당신의 도움을 많이 감사합니다. 내가 약간의 변화 즉, "~ /"를해서 getJSON 방법은 \t을 작동시킬 수에 추가했다 \t $ .getJSON ("<% = Html.AbsolutePath ("~/ApplicationSettings/Machines/List/") %>" – user99513

0

드롭 ".ready $ (문서) (함수() {[YOUR_CODE]});" 블록? 그렇지 않은 경우 DOM은 아직 준비가되지 않았을 것입니다.)

+0

알다시피 나는 $ (문서) .ready (....) 블록 내에서 기능을 가지고있다. 나는 IE8와 함께 제공되는 스크립트 디버깅 도구를 사용하여 스크립트를 통해 강화하며 라인 $ (문서) .ready (() 함수에서 "개체 예상"라는 오류가 발생합니다 .. 어떤 아이디어? – user99513

+0

그것은 아마의 littlechris와 같은 스크립트 경로 지정 문제가 지적되었습니다. $가 정의되지 않은 것 같습니다 (이 시점에서 정의되지 않은 유일한 것임). 부수적으로, 적절한 디버깅을 위해 firefox + firebug를 사용하는 것이 좋습니다 (IE8은 슬픈 농담입니다.). –

0

조치 코드는 어떻게 작성 했습니까? 이는 서버의 가상 디렉터리에서 실행하는 경우 중요 할 수 있습니다. 로컬로 http://localhost:xxxx/controller/action으로 실행하지만 원격으로 http://mysever/myapp/controller/action으로 실행하는 경우 Url.Action()을 사용하여 작업 결과의 실제 경로를 확인해야합니다.

관련 문제