2013-05-02 101 views
2

我是jquery的新成員ajax.i想調用Web服務但不工作。 這是我的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"); } 



       }); 

      }); 

這是WebService方法。

[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中使用FireBug按F12)查看正在請求並返回的內容?你有沒有看過錯誤細節? – Corey 2013-05-02 05:27:18

+0

無法加載資源:服務器響應狀態爲500(內部服務器錯誤)我在Web瀏覽器中出現此錯誤。 – 2013-05-02 05:37:18

+0

通常這是因爲您的代碼中引發了異常。嘗試直接瀏覽Web服務URL並查看是否收到異常跟蹤消息。另外,請嘗試調試Web服務,並在'GetInceDed'方法的開頭添加一個斷點。 – Corey 2013-05-02 05:46:50

回答

4

您還沒有公佈確切的錯誤信息,但也有幾件事情來尋找:

  1. 請注意,您在您的通話$.ajax指定POST,而你的ScriptMethodUseHttpGet = true。我假設了POST

  2. 包含Web類/腳本方法必須有[System.Web.Script.Services.ScriptService]以可調用從阿賈克斯(按照由asmx代碼模板添加的註釋)

下面的服務器代碼對我的作品:

[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 2013-05-02 05:25:11

+0

是啊,我知道,但使用.Net只能這樣工作,幾周前,我遇到了同樣的問題,我嘗試了在互聯網上找到的每種方式, – 2013-05-02 05:37:31