1

I'm trying to call a code-behind method on the click event for this button:

<input type="button" id="btn_submit1" class="btn btn-default" value="Calculate" />

Here's the AJAX call for it:

$(document).ready(function() {

    $("#btn_submit1").click(function() {

        var msg = $("#txt_1_1").val();

        $.ajax({
            type: 'POST',
            url: 'Calc.aspx/calculate',
            data: '{sText:' + msg + '}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function(response) {
                alert("success!!");
                alert(response.d);
                $("#test").html(response.d);
            },
            failure: function(response) {
                alert(response.d);
            }
        });
    });

});

Here's the WebMethod:

[System.Web.Services.WebMethod]
    public static string calculate(string sText)
    {
        System.Diagnostics.Debug.WriteLine("sText = " + sText);
        return sText;
    }

Both the alerts in the success part are being called, but the second one says "undefined" and so nothing is getting displayed in the div "test". I've searched a lot but couldn't find what's wrong with the code.

And also, Debug.WriteLine does not print anything in the Output window. I used this after searching through the net, I'm not sure if it's the right way to print or not.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
ak92
  • 41
  • 9
  • Presumably then `response.d` is undefined. What is in `response`? Why do you expect it to have a `d` property? – David Jun 19 '15 at 15:15
  • `"Debug.WriteLine does not print anything in the Output window"` - What output window? This is a web application, they generally don't have console windows associated with them. – David Jun 19 '15 at 15:16
  • @Archer - console.log(data) shows this error in Chrome console - Uncaught ReferenceError: data is not defined However console.log(msg) displays whatever I had in the textbox "txt_1_1". – ak92 Jun 19 '15 at 15:27
  • Sorry - my bad (force of habit). Change it to `console.log(response);` It will show you exactly what is being returned, rather than [object Object] – Reinstate Monica Cellio Jun 19 '15 at 15:28
  • @Archer - Here's the message in console for console.log(response): Object {Message: "Authentication failed.", StackTrace: null, ExceptionType: "System.InvalidOperationException"} – ak92 Jun 19 '15 at 15:32
  • @David: Yes, I read that Console.Writeline would not work, hence used this one. – ak92 Jun 19 '15 at 15:35
  • @connoisseur: Right, but where do you expect `Debug.Writeline()` to output? And your other comment indicates that an exception is being thrown server-side. Which line throws that exception? What are the details of that exception? – David Jun 19 '15 at 15:37
  • Have a look here - it may help... http://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized – Reinstate Monica Cellio Jun 19 '15 at 15:39
  • @Archer - Nope. Got This: POST https://localhost:44300/Calc.aspx/calculate 500 (Internal Server Error) send @ jquery-1.10.2.js:8720 jQuery.extend.ajax @ jquery-1.10.2.js:8150 (anonymous function) @ Calc.aspx:114 jQuery.event.dispatch @ jquery-1.10.2.js:5109 elemData.handle @ jquery-1.10.2.js:4780 – ak92 Jun 19 '15 at 15:50
  • @David - I'm sorry, I'm a beginner, perhaps I can get the details if you elaborate where to get it from. The Chrome console however points to this line: console.log(response) – ak92 Jun 19 '15 at 15:57
  • The server error could be caused by many things, but one that leaps out is the structure of the data you're sending. Change it to `data: JSON.stringify({ sText: msg }),` You're currently passing a string with no delimiters around it. – Reinstate Monica Cellio Jun 19 '15 at 16:09
  • @Archer - Still the same result.. :( I think If I could find what the "calculate" WebMethod is receiving in the parameter, it could help further narrowing down the issue.. but Debug.WriteLine won't print anything. – ak92 Jun 19 '15 at 16:39
  • Put a breakpoint on that line so you can see what's going on. – Reinstate Monica Cellio Jun 22 '15 at 18:14

1 Answers1

0

The ".d" part of the response is a security feature that Microsoft added to ASP.NET 3.5 to encapsulate a JSON object. You can check if you need to use the ".d" by doing:

if (response.hasOwnProperty("d")) {
    console.log(response.d);
    var myData = $.parseJSON(response.d);
} else {
    console.log(response);
}

Edit: It's likely that in this case you do not need to use the ".d" because the data you are returning is not valid JSON and most likely a string.

Edit 2: From the internal server error you've mentioned in a comment it looks like your data may not be in the correct format. I'm just speculating here really but you might try:

var datapackage = {
    msg: $("#txt_1_1").val()
};
var datapacked = JSON.stringify(datapackage);

And then on the the AJAX call put "data: datapacked,".

Drummad
  • 722
  • 1
  • 6
  • 23
  • Object {Message: "Authentication failed.", StackTrace: null, ExceptionType: "System.InvalidOperationException"} – ak92 Jun 19 '15 at 15:38
  • Then I'm afraid it looks like the issue is with your ASP WebMethod. You might check out the answer to this question here: http://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized – Drummad Jun 19 '15 at 16:23
  • Did not help. Got this error: POST localhost:44300/Calc.aspx/calculate 500 (Internal Server Error) send @ jquery-1.10.2.js:8720 jQuery.extend.ajax @ jquery-1.10.2.js:8150 (anonymous function) @ Calc.aspx:114 jQuery.event.dispatch @ jquery-1.10.2.js:5109 elemData.handle @ jquery-1.10.2.js:4780 – ak92 Jun 19 '15 at 16:40
  • I've edited my answer again with something you could try... it may be that the error is resulting from incorrect JSON being passed in. – Drummad Jun 19 '15 at 17:02
  • Nope. Still the same error. This is making me crazy now :@ – ak92 Jun 20 '15 at 15:17