2014-09-28 2 views
1

나는 다음과 같은 AngularJS와 코드를 가지고 :

$http.get("/Home/GetEmails").then(function (response) { 
      $scope.emails = response.data; 
      $scope.init($scope.emails); 
      $scope.totalEmails = $scope.emails.length; 
     }); 

내가 로컬 개발할 때, 그것은 잘 작동하지만 라이브 서버에 게시 할 때, 그것은 다음과 같은 오류 메시지가 있습니다 :

Failed to load resource: the server responded with a status of 404 (Not Found)합니다. http://xaisoft.com/Home/GetEmails을 찾고 있지만 찾을 수 없습니다. 이 기능을 사용하려면 ASP.NET MVC 및/또는 Angular에서해야 할 일이 있습니까? 현재, GetEmails 액션은 내 HomeController에서 JSON 객체를 반환합니다. 당신이 그랬던 것처럼

HomeController

public class HomeController : Controller 
{ 
    // 
    // GET: /Home/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult GetEmails() 
    { 
     return Json(new[] 
     { 
      new 
      { 
       from = "Jet Blue", 
       to = "Me", 
       subject = "Buy one seat, get one free!", 
       body = "That and helping Data finish 'Pop Goes the Weasel'. Also counts as a CMOH.", 
       date = "Dec 20th 12:22 PM", 
       isImportant = false, 
       isStarred = true, 
       isChecked = false, 
       isRead = true 
      }, 
      new 
      { 
       from = "Publix", 
       to = "Me", 
       subject = "Check this weeks BOGO deals", 
       body = "Hurry, ends Thursday!", 
       date = "Mar 15th 8:15 AM", 
       isImportant = false, 
       isStarred = false, 
       isChecked = false, 
       isRead = false 
      }, 
      new 
      { 
       from = "AJ Carpio", 
       to = "Me", 
       subject = "When Life Gives You Questions, Google has Answers", 
       body = "Get more life quotes here", 
       date = "Mar 15th 8:15 AM", 
       isImportant = true, 
       isStarred = false, 
       isChecked = false, 
       isRead = true 
      } 
     },JsonRequestBehavior.AllowGet); 
    } 

} 
+1

당신이 404을 반환하는 브라우저에서 해당 URL을 방문하는 경우, 그래서 난을 게시하시기 바랍니다 AngularJS보다는 .NET 코드. 각도는 문제와 관련이없는 것 같습니다. –

+0

@DanBowling - ASP.NET MVC HomeController를 게시했습니다. – xaisoft

답변

2

당신은 ASP.NET MVC 응용 프로그램에서 URL을 하드 코딩해서는 안 :

$http.get("/Home/GetEmails") 

당신은 항상 URL 헬퍼를 사용해야합니다.

<script type="text/javascript"> 
    var getEmailsUrl = @Url.Action("GetEmails", "Home"); 
</script> 

나중에 NG 스크립트에서 사용할 수 있습니다 :보기에 예를 들어, 당신은 변수를 설정할 수 있습니다

$http.get(getEmailsUrl) 
같은 Url.Action 같은 URL 헬퍼의 목적은 같은 계정 것들 고려하는 것입니다

호스팅 환경 가상 디렉터리 및 라우팅 구성 이러한 모든 요인은 다양한 환경 사이에서 바뀔 수 있으며 자바 스크립트에서 URL을 하드 코딩하면 404 개를 많이 얻게됩니다.


여기에는 글로벌 자바 스크립트 변수를 등록하는 대신이 작업을 수행하는 각도가 더 넓습니다.

<script type="text/javascript"> 
    angular.module('myApp').config(['$provide', function ($provide) { 
     // Here you can also register a complex javascript object that will serve 
     // as configuration for your app and can contain multiple urls 

     $provide.value('getEmailsUrl', '@Url.Action("GetEmails", "Home")'); 
    }]); 
</script> 

다음 당신은 당신의 컨트롤러와 서비스의 getEmailsUrl를 주입 할 수 있습니다 : 당신은 $provide 사용할 수

app.controller('HomeController', ['$scope', 'getEmailsUrl', function ($scope, getEmailsUrl) { 
    // You can use the getEmailsUrl variable here. 
}]); 
+0

안녕하세요, 내보기에서 URL 도우미를 사용했지만 외부 js 파일에서 http.get을 호출 했으므로이 태그를 넣으려고하면 var getEmailsUrl = @ Url.Action ("GetEmails", "Home");', 그것은 불법 문법이라고 불평합니다. – xaisoft

+0

아마도 내 대답을 신중하게 읽지 않았을 것입니다. 나는 당신의 면도기 뷰 안의 **

관련 문제