2016-12-20 3 views
0

나는이 오류와 함께 화를 낼 것입니다. POST 500 내부 서버 오류

POST http://localhost:56105/Home/getContactbyId?ConId=%225%22 500 (Internal Server Error)

당신이 날 도울 수있을 것입니다 바랍니다. ContactId를 기반으로 연락처 데이터를 가져와야합니다. 관련 코드 (내가 뭔가를 놓친 경우 필요한 경우 사과, 내가 추가 할 것입니다) 다음과 같은 :

contactController.js

ContactsApp.controller("contactController", function ($scope, $rootScope, $routeParams, $location, myService) { 
    function getContactById(id) { 
     var getting = myService.get_Contact_By_Id(id);  
     getting.then(function successCallback(response) { 
      $rootScope.Name = response.data.Name; 
     }, function errorCallback(response) { 
         return "Error"; 
           }); 
       } 
     function init() { 
     var contactId = $location.path().split("/")[2]; 
        console.log(contactId); //this loggs the correct Id 
     getContactById(contactId); 
    } 
    init(); 
} 

myService.js

ContactsApp.service("myService", function ($http) { 
    this.get_Contact_By_Id = function (ConId) { 
    console.log(ConId); //this logs the correct id 
    return $http({ 
     method: 'post', 
     url: '/Home/getContactById', 
     params: { 
      ConId: JSON.stringify(ConId) 
     } 
    }); 
    } 
} 

HomeController.cs

public class HomeController : Controller 
{ 
    public string getContactById(int ConId) 
    { 
     int id_int = Convert.ToInt32(ConId); 
     using (ContactsDBEntities contactsData = new ContactsDBEntities()) 
     { 
      var theOne = contactsData.Contacts.Where(x => x.Id == id_int).FirstOrDefault(); 
      return theOne.Name; 
     } 
    } 
} 
+4

는 HTTP 오류를 일으키는 확인하기 위해 서버 응용 프로그램 로그 (응용 프로그램 이벤트 뷰어)를 확인해야합니다. 서버 측 코드에 중단 점을 설정하여 오류가 발생한 위치를 확인하거나 예외를 기록하려고 시도 했습니까? – ADyson

+1

@ADyson이 지적했듯이 서버 측 코드를 디버깅해야합니다. 중단 점을 설정하고 IDE를 연결하십시오. 그럴 수 없다면 로깅을 추가하십시오. – Igor

+0

@DannyFardyJhonstonBermúdez가 위에서 말한대로 ConId 주위에 따옴표가 추가 된 것 같습니다. 디코딩 된 URL - http : // localhost : 56105/Home/getContactbyId? ConId = "5" – KieranDotCo

답변

2

JSON.stringify(ConId)을 사용하면 ConId 번호. paramsname : value 쌍의 간단한 개체를 필요로합니다. ConIdstringify() 사용하여 전송하면 간단하게

params: { 
      ConId: ConId 
     } 

를 사용 Convert.ToInt32(ConId) 호출 변환에 대한 예외 (예상 번호가 아닌 '번호')를 발생합니다. 해당 변환이나 try-catch 블록을 사용한 후에 유효성을 검사해야합니다.

결과가없는 경우 FirstOrDefault()을 반환해도 문제가 될 수 있습니다. 변수를 사용하기 전에 theOne 변수도 확인해야합니다.

당신은 예외가 (500)의 상태가 서버 측에 오류를 의미 500