JQuery, ajax POST method returns success: Undefined

my script code:

$('#btnSave').click(function() {
    var pageUrl = '<%= ResolveUrl("~/TestPage.aspx/SystemEdit")%>';
    var ip = $('#editIP').text();
    var loc = $('#txtBay').val();
    var team = $('#txtTeam').val();
    var port = $('#txtPort').val();
    var xcel = "", office = "", moni = "";                                   
    var parameter={ "ip": ip, "loc": loc, "team": team, "port": port, "excel": xcel, "office": office, "monitor": moni}

    $.ajax({
        type: 'POST',
        url: pageUrl,
        data: JSON.stringify(parameter),
        contentType: 'json',
        success: function(data) {
            alert(data);
        },
        error: function(data,success,error) {
            alert("Error:" +error);
        }
    });           
});

my code is behind C # code:

[WebMethod]
public static string SystemEdit(string ip, string loc,string team, string port, string excel,string office, string monitor)
{
    return "The Current Time is: "+ DateTime.Now.ToString();
}

my page name: TestPage.aspx

When I click the save button, I get 'undefined'. I am not getting the current time from code in C #.

+4
source share
3 answers

You need to return json result as below:

return JsonConvert.SerializeObject("The Current Time is: "+ DateTime.Now.ToString());

Also added below is the method described above:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

And as you defined the json format, you should write:

 contentType: "application/json; charset=utf-8",

By the way, you should use the Webservice here!

+1
source

I assume that the content type setting jsonshould be done as follows:

contentType: 'application/json',
0
source

vs2013, , route.config, .

 'settings.AutoRedirectMode = RedirectMode.Permanent

VB:

  <WebMethod()>


       Public Shared Function GetReport(ByVal Data As String) As String
          Try

                Return "Hello" + Data

            Catch ex As Exception

                Return "Failed"
      End Try
End Function

Js Script:

$('#btnSave').click(function () {
  var char = $(this).text();

 var SendData = {};
  $.ajax({
      type: "POST",
      url: "TEST.aspx/GetReport",
      data: JSON.stringify(SendData),
      data: "{ 'Data': '" + char + "'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (data) {
          $('#lbl_test').text(data.d);
      },
      error: function (data, success, error) {
          alert("Error:" + error);
      }
  });
 });
0

All Articles