【问题标题】:how to call jquery ajax to c# page如何将jquery ajax调用到c#页面
【发布时间】:2015-08-08 04:41:03
【问题描述】:

如何调用 jquery ajax 到 c# 页面没有来自 Visual Studio 的响应尝试通过断点

首先尝试使用 Big one,C# 端(ProfitCentersNameInsert)函数没有响应,然后尝试了另一个简单类型(应用)函数,但两种方法都没有响应 刚刚在 Ajax 上评论了以下几行

//    url: "ProfitCentersScreen.aspx/ProfitCentersNameInsert",
//  data: "{'ProfitCenterName':'" + ProfitCenterName + "'}",

JS代码

$(function() {
    profit.onRefresh()
    $( "#btnAdd" ).click(profit.onClickSave);
});

var profit = {
    onRefresh: function(){},

    onClickSave: function(){
        var ProfitCenterName =$('#txtProfitCenter').val();

        $.ajax({ 
            type: 'POST',
            contentType: "application/json; charset=utf-8",
        //    url: "ProfitCentersScreen.aspx/ProfitCentersNameInsert",
            url: "ProfitCentersScreen.aspx/apply",
          //  data: "{'ProfitCenterName':'" + ProfitCenterName + "'}",
            data:'{}',

            async: false,
            success: function (response) {
                alert( response );
                alert(ProfitCenterName);
                $('#txtProfitCenter').val('');
                alert("Record saved successfully..!!");
            },
            error: function () { 
                alert("Error");
            }
        }); 
    }   
};     

C#代码

[WebMethod]
    public static string ProfitCentersNameInsert(string ProfitCenterName)
    {

        NpgsqlConnection conn = new NpgsqlConnection("Server=192.168.0.133;User Id=hll; " + "Password=hll;Database=checking_DB;"); //+ "Pooling=true;MaxPoolSize=100;Timeout=20;"
        try
        {
            conn.Open();
            NpgsqlCommand command = new NpgsqlCommand("Insert into tbl_Profit_Centers(Profit_Center_Id,Profit_center_Name) Values(default,@ProfitCenterName)", conn);
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@ProfitCenterName", ProfitCenterName);
            command.ExecuteNonQuery();
            conn.Close();
            return "Success";
        }
        catch (Exception ex)
        {
            return "failure";
        }

    }


    [WebMethod]
    public static string apply()//method must be "pulic static" if it is in aspx page
    {
        return "Hi";
    }

【问题讨论】:

  • 如果您使用的是 firefox,请尝试右键单击您的网页,单击检查元素,然后检查网络选项卡,然后执行任何应该触发您的帖子的操作,看看它是否显示在网络中标签
  • 你有没有在部分声明jquery库?

标签: c# jquery asp.net asp.net-ajax c#-2.0


【解决方案1】:

试试这个

 url: "ProfitCentersScreen.aspx",
 data: {ProfitCenterName: ProfitCenterName}

【讨论】:

    【解决方案2】:

    尝试以下几点,它应该可以工作:

    1) 如果您的页面位于项目的根级别,则 url 应如下所示:

    url: "/ProfitCentersScreen.aspx/apply"
    

    url: "../ProfitCentersScreen.aspx/apply"
    

    2) 字符串化你的参数

    data: JSON.stringify({})
    

    3) 最后加上这个参数

    dataType: "json",
    

    【讨论】:

      【解决方案3】:

      尝试使用response.d 取得成功

       success: function (response) {
                      alert(response.d);
                      alert("Record saved successfully..!!");
                  },
      

      这是一个简单的 jQuery Ajax 调用的伪代码

      jQuery:

       $("#bntAJax").on('click', function (e) {
                  
                  $.ajax({
                      type: "POST",
                      // your  webmethod   
                      url: "AjaxFunction/myFunction.asmx/apply",
                      data: '{}',
                      contentType: "application/json; charset=utf-8",
                      dataType: "json", // dataType is json format
                      success: OnSuccess,
                      error: OnErrorCall
                  });
          
                  function OnSuccess(response) {
                      alert(response.d);
                  }
          
                  function OnErrorCall() {
                 
                  }
                  e.preventDefault();
          });
      

      网络方法:

        [WebMethod]
          public string apply()
          {
              return "Hello";
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多