繁体   English   中英

jQuery ajax调用似乎根本不执行任何操作

[英]jQuery ajax call doesn't seem to do anything at all

我在jQuery中进行Ajax调用时遇到问题。 完成了一百万次之后,我知道我在这里确实错过了一些愚蠢的事情。 这是我进行ajax调用的javascript代码:

   function editEmployee(id) {
       $('#<%= imgNewEmployeeWait.ClientID %>').hide();
       $('#divAddNewEmployeeDialog input[type=text]').val('');
       $('#divAddNewEmployeeDialog select option:first-child').attr("selected", "selected");
       $('#divAddNewEmployeeDialog').dialog('open');
       $('#createEditEmployeeId').text(id);
       var inputEmp = {};
       inputEmp.id = id;
       var jsonInputEmp = JSON.stringify(inputEmp);
       debugger;
       alert('Before Ajax Call!');
       $.ajax({
           type: "POST",
           url: "Configuration.aspx/GetEmployee",
           data: jsonInputEmp,
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (msg) {
               alert('success');
           },
           error: function (msg) {
               alert('failure');
           }
       });
   }

这是我试图调用的CS代码:

    [WebMethod]
    public static string GetEmployee(int id)
    {
        var employee = new Employee(id);

        return Newtonsoft.Json.JsonConvert.SerializeObject(employee, Newtonsoft.Json.Formatting.Indented);
    }

当我尝试运行此命令时,确实收到警告,提示“ Before Ajax Call! 但是,我永远不会收到表示success的警报或表示failure的警报。 我确实进入了我的CS代码,并在GetEmployee方法上设置了一个断点。 断点确实命中,所以我知道jQuery成功调用了该方法。 我逐步介绍了该方法,它执行得很好,没有错误。 我只能假设当jQuery ajax调用从该调用返回时发生错误。

另外,我查看了事件日志只是为了确保没有发生ASPX错误。 日志中没有错误。 我也看了看控制台。 没有脚本错误。 有人对我在这里缺少什么有任何想法吗?

`

尝试这个

function editEmployee(id) {
   $('#<%= imgNewEmployeeWait.ClientID %>').hide();
   $('#divAddNewEmployeeDialog input[type=text]').val('');
   $('#divAddNewEmployeeDialog select option:first-child').attr("selected", "selected");
   $('#divAddNewEmployeeDialog').dialog('open');
   $('#createEditEmployeeId').text(id);
   //var inputEmp = {};
  // inputEmp.id = id;
  // var jsonInputEmp = JSON.stringify(inputEmp);
   //debugger;
   alert('Before Ajax Call!');
   $.ajax({
       type: "POST",
       url: "Configuration.aspx/GetEmployee",
       data: {id: id},
      // contentType: "application/json; charset=utf-8",
      // dataType: "json",
       success: function (msg) {
           alert('success');
       },
       error: function (msg) {
           alert('failure');
       }
   });   }

如果ajax调用没有进入成功函数,则问题出在CS代码中的数据格式。 返回数据不能采用正确的JSON格式。 我猜你应该得到“ 500”错误。

尝试以正确的JSON格式返回它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM