lildakota
lildakota

Reputation: 129

Ajax call to code behind. Reaching complete stage, but not returning success or triggering code behind function

I'm attempting to trigger a code behind method from AJAX:

function onEnter(key) {
  var lotTxt = document.getElementById("lotTxt");
  if (key.keyCode == 13) {
    alert("in if")
    $.ajax({
      url: 'Default.aspx/assEatinSzn',
      type: "POST",
      contentType: 'application/json; charset=utf-8',
      data: { test: "testString"},
      dataType: 'json',
      success: function (data) {
        alert("Success")
      },
      failure: function (data) {
        alert("Failure")
      }
    });
  }
}

All I am attempting to do in the code behind is update this text box with the value below:

Public Sub assEatinSzn(test As String)
  productTxt.Value = "Success"
End Sub

Essentially I am asking why the ajax is not triggering the code behind method. I have put break points to see if the the code behind method is ever triggered, but nothing ever breaks. The ajax method will produce the complete function as well in a weird side effect. Thanks in advance

Upvotes: 1

Views: 137

Answers (1)

Aousaf Rashid
Aousaf Rashid

Reputation: 5758

The problem is you are not passing a value for the test parameter.Hope the below code will solve your issue :

  type: 'POST',
  url: 'default.aspx/assEatinSzn',
  data: JSON.stringify({ test: 'value here' }),
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
  success: function (data) {
  }
  ......

And make sure to add [WebMethod] before your method :

 [System.Web.Services.WebMethod]
 Public Shared Sub assEatinSzn(test As String)
 productTxt.Value = "Success"
 End Sub

Upvotes: 1

Related Questions