0

I'm getting the result, "undefined", from my AJAX call in the success block.

  1. AJAX calls a web method in my cs file, while passing a vale (a date).
  2. My web method build s a calendar from the date and passes all of the HTML code back.
  3. The success function does not pick up the data passed from the web method.

I have used the exact same code on a few other web apps, and it works great, just not on this one. The difference is, I'm not using a blank .net web forms build, but a template with bootstrap. I have verified that the web method is correctly building the calendar text/HTML, it's just not getting read by the result.d in the AJAX success. I have looked through other posts of users having the same issue, but none of the solutions worked for me.

$.ajax({
         type: "POST",
         url: "Calendar.aspx/getCal",
         data: "{'dateParam': '" + newDate + "'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
            success: function (retValue)
            {
                var myReturn = retValue.d;
                document.getElementById("divPh").innerHTML = myReturn;
            }                    ,
            error: function (data) {
                 alert('error: ' + data.status);
            }
        });

//Web Method on Calendar.aspx.cs page. Pulled out complicated code to simply
//show a hard-coded return.
[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public static string getCal(string dateParam)
{
     string retValue = "This Sucks";
     return retValue;
}
MotleyDrew
  • 51
  • 6
  • 1
    Are you sure it's not in `retValue` itself? – Jimenemex Apr 10 '19 at 18:34
  • console.log(retValue); and see what you get in the console – gmdev86 Apr 10 '19 at 18:34
  • 1
    Thanks for trying to create [MCVE], but ".d" part in `retValue.d` look very strange - make sure you've updated both parts of the sample to match. – Alexei Levenkov Apr 10 '19 at 18:35
  • Your ajax's `dataType` is set to `json`, yet you are returning html as a string? I think that could prove problematic. (https://stackoverflow.com/questions/2722750/ajax-datatype) – Ryan Wilson Apr 10 '19 at 18:37
  • Jimenmex, yes, it's not in retValue itself. The return from retValue is [object Object]. – MotleyDrew Apr 10 '19 at 18:57
  • Alexei Levenkov , that is irrelevant. For the sake of the example, I should have given them different names, as the retValue in the web method is not the same var as the retValue in the AJAX call. I can see how that miight come across as confusing. – MotleyDrew Apr 10 '19 at 19:02
  • I have one small example like yours and it's working. https://github.com/kblok/StackOverflowExamples/blob/master/AspNetDemoProject/AspNetDemoProject/Demos/WebMethodTest.aspx.cs#L38 – hardkoded Apr 10 '19 at 19:03

0 Answers0