【问题标题】:$.ajax not calling WebService$.ajax 没有调用 WebService
【发布时间】:2013-05-02 04:50:00
【问题描述】:

我是 jquery ajax 的新手。我想调用 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;

    }

这在我调用时不起作用。它执行错误部分。 请帮助我。我做错了什么。

【问题讨论】:

  • 您是否使用过浏览器中的开发者工具(在 IE、Chrome 或 Firefox-with-FireBug 中按 F12)查看请求和返回的内容?您是否查看过错误详细信息?
  • 加载资源失败:服务器响应状态为 500(内部服务器错误)我在 Web 浏览器中收到此错误。
  • 这通常是因为您的代码中抛出了异常。尝试直接浏览到 Web 服务 URL 并查看是否收到异常跟踪消息。另外,尝试调试 Web 服务并在 GetInceDed 方法的开头放置一个断点。
  • web 服务方法没有被断点击中。当我在方法开始时超过断点。

标签: c# web-services jquery


【解决方案1】:

您尚未发布确切的错误消息,但有几件事需要查找:

  1. 请注意,您在 $.ajax 调用中指定了 POST,而您的 ScriptMethod 具有 UseHttpGet = true。我假设POST

  2. 包含 Web / Script 方法的类必须具有 [System.Web.Script.Services.ScriptService] 才能从 ajax 调用(根据 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}"}

【讨论】:

    【解决方案2】:

    尝试这些更改:

    $(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;
    
        }
    

    【讨论】:

    • 手动组装 JSON 字符串通常是个坏主意。序列化类和方法的存在是有原因的,并且在一般情况下工作得很好。
    • 是的,我知道,但是使用 .Net 只能以这种方式工作,几周前,我遇到了同样的问题,我尝试了我在互联网上找到的所有方法,但都没有成功
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多