0

How to call jquery ajax to c# page there is no response from visual studio tried through break point

first tried with Big one no response from C# side(ProfitCentersNameInsert) function then tried another simple type (apply) function but no response from both methods just commented the below lines on Ajax

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

JS Code

$(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# Code

[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";
    }
  • if you are using firefox try right clicking your web page, click inspect element and then check the network tab then do whatever it is that is supposed to fire your post and see if it shows up in the network tab – Jeremy C. May 26 '15 at 13:32
  • Have you declare the jquery library in the section? – Conrad Lotz May 26 '15 at 13:37
  • Maybe this could help you http://stackoverflow.com/questions/30082988/function-not-defined-error-while-using-asp-net-ajax/30083658#30083658 – Enrique Zavaleta May 26 '15 at 14:04

3 Answers3

0

try This

 url: "ProfitCentersScreen.aspx",
 data: {ProfitCenterName: ProfitCenterName}
Zohaib Waqar
  • 1,204
  • 11
  • 18
0

Try using response.d in your success

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

Here's pseudo code a simple jQuery Ajax call

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 :

  [WebMethod]
    public string apply()
    {
        return "Hello";
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
0

Try below points, it should work :

1) if your page is at root level of project then the url should be like below :

url: "/ProfitCentersScreen.aspx/apply"

OR

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

2) stringify your parameter

data: JSON.stringify({})

3) Finally add this parameter

dataType: "json",
Rakesh Sajja
  • 148
  • 9