2013-05-02 106 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