2013-05-02 2 views
2

jquery ajax.i에서 새로운 기능이지만 웹 서비스를 호출하려고하지만 작동하지 않습니다. 이것은 내 jquery 코드입니다.

$(document).ready(function() { 
      $('#TxBx_BasicSalary').focusout(function() { 
       var EmployeeId = $('#Hid_EmpID').val(); 

       $.ajax({ 
        type: "POST", 
        cache: false, 
        contentType: "application/json; charset=utf-8", 
        url: '/WebService/IncDedWebService.asmx/GetInceDed', 
        data: JSON.stringify({ id: EmployeeId }), 
        dataType: 'json', 
        success: function (data) { 
         alert("") 

        }, 
        error: function() { alert("error"); } 



       }); 

      }); 

이것은 웹 서비스 메서드입니다.

[WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public string GetInceDed(int id) 
    { 
     ClsSalary salary = new ClsSalary(); 
     //var abc salary=.GetIncDedByEmpId(id); 

     var serializer = new JavaScriptSerializer(); 
     var json = serializer.Serialize(salary.GetIncDedByEmpId(id)); 
     return json; 

    } 

내가 전화를 걸 때이 기능이 작동하지 않습니다. 오류 부분을 실행하십시오. 도와주세요. 내가 뭘 잘못했는지.

+0

브라우저에서 개발자 도구를 사용 했습니까 (IE, Chrome 또는 Firefox-with-FireBug의 F12 키 누름) 요청한 항목과 반환 된 항목을 확인 했습니까? 오류 세부 사항을 전혀 보지 않았습니까? – Corey

+0

리소스를로드하지 못했습니다 : 서버가 500 (내부 서버 오류) 상태로 응답했습니다. 웹 브라우저에서이 오류가 발생했습니다. –

+0

종종 코드에 예외가 발생했기 때문입니다. 웹 서비스 URL을 직접 찾아보고 예외 추적 메시지가 있는지 확인하십시오. 또한 웹 서비스를 디버깅하고 GetInceDed 메소드의 시작 부분에 중단 점을 넣으십시오. – Corey

답변

4

당신은 정확한 오류 메시지를 게시하지 않은,하지만 찾기 위해 몇 가지가있다 : 당신이 당신의 $.ajax 호출 POST을 지정한

  1. Note을 ScriptMethod 반면, UseHttpGet = true입니다. 내가 POST라고 가정했습니다.

  2. 웹을 포함하는 클래스/스크립트 방법은 아약스에서

다음 서버 코드는 나를 위해 작동합니다 (asmx 코드 템플릿에 의해 추가 된 주석에 따라) 호출 될하기 위해 [System.Web.Script.Services.ScriptService]이 있어야합니다 :

[WebService(Namespace = "http://YourNameSpaceGoesHere/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 
public class IncDedWebService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
    public string GetInceDed(int id) 
    { 
     ClsSalary salary = new ClsSalary(); 
     //var abc salary=.GetIncDedByEmpId(id); 

     var serializer = new JavaScriptSerializer(); 
     var json = serializer.Serialize(new ClsSalary 
              { 
               Amount = 1234, 
               Id = 123, 
               Name = "Basic Salary" 
              }); 
     return json; 
    } 
} 

public class ClsSalary 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public decimal Amount { get; set; } 
} 

json으로는 반환됩니다

{"d":"{\"Id\":123,\"Name\":\"Basic Salary\",\"Amount\":1234}"} 
1

이러한 변경을 시도 :

$(document).ready(function() { 
      $('#TxBx_BasicSalary').focusout(function() { 
       var EmployeeId = $('#Hid_EmpID').val(); 

      $.ajax({ 
       type: "POST", 
       cache: false, 
       contentType: "application/json; charset=utf-8", 
       url: '/WebService/IncDedWebService.asmx/GetInceDed', 
       data: '{ "id": "' + EmployeeId + '" }', //THIS LINE 
       dataType: 'json', 
       success: function (data) { 
        var emp = $.toJson(data.d); //THIS 
        alert(emp.IncDeb.EmpID); //AND THIS 

       }, 
       error: function() { alert("error"); } 



      }); 

     }); 

이 WebService에 방법이다.

[WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public string GetInceDed(int id) 
    { 
     ClsSalary salary = new ClsSalary(); 
     var abc salary=.GetIncDedByEmpId(id); 
     string json = "{\"IncDeb\":[\"EmpId\":\"" + abc.EmpId +"\"]"; //And you have to keep going with the other members 
     return json; 

    } 
+0

JSON 문자열을 수동으로 어셈블하는 것은 일반적으로 좋지 않습니다. 직렬화 클래스와 메소드는 이유가 있기 때문에 일반적으로 잘 작동합니다. – Corey

+0

그래, 나도 알아,. NET을 사용하면 이런 식으로 작동한다. 몇 주 전, 나는 똑같은 문제를 겪었고, 나는 인터넷에서 찾은 모든 방법을 시도해 보았다. –

관련 문제