1

I have the following code in my aspx.cs file:

[System.Web.Services.WebMethod]
public static string PostPin() {
    return "Rahul";
}

And the following Javascript:

$.ajax({
type: "POST",
url: "/adfs/ls/RequestPinForm.aspx/PostPin",
contentType: "application/json; charset=utf-8",
success: function(data) {
    alert(data);
},
error : function(data , data2 , data3) {
    alert(data);
}
});

I'm using the alert's to see the returns of what is coming back, but I can never get it to return the string.

Any ideas guys?

Idiot211
  • 848
  • 7
  • 17
  • When I alert the data, the data returned is the HTML for the page, is that normal? It's not JSON return but the HTML of the page itself. – Idiot211 Oct 06 '15 at 09:04
  • 1
    Use the `[ScriptMethod(ResponseFormat = ResponseFormat.Json)]` attribute to see if it returns a JSON format – Izzy Oct 06 '15 at 09:05
  • ya this functionality is working perfect and return html as expected....all you need to do is serialize the output (here "rahul") and then return –  Oct 06 '15 at 09:06
  • Please add dataType="json" in your ajax call – Sain Pradeep Oct 06 '15 at 09:10
  • 1
    See this http://stackoverflow.com/questions/27917255/c-sharp-web-method-is-not-calling-in-javascript/27917333#27917333 – Mairaj Ahmad Oct 06 '15 at 09:15
  • @Izzy -- That did not return JSON. – Idiot211 Oct 06 '15 at 09:22
  • @PranavPatel -- That is not correct, I do not want the HTML. I want the output which is being returned. My output is not part of the HTML. – Idiot211 Oct 06 '15 at 09:22
  • @Idiot211 Have you tried `alert(data.d);`?. I've just tried your code with `alert(data.d);` and it returns `Rahul` – Izzy Oct 06 '15 at 09:22
  • @Idiot211 i just tell you only the currunt code functionaliy....and also tell you that need to serialize the object and return that object and that will be diaplay as either json or xml as your scriptmethod was seted –  Oct 06 '15 at 09:24
  • @Izzy -- I just tried it and I'm getting undefined, this is really weird. I'll investigate. – Idiot211 Oct 06 '15 at 09:25
  • @Idiot211 check in the browser console to see if there's any error messages showing – Izzy Oct 06 '15 at 09:26
  • @Izzy -- No error messages, just an AJAX call that returns the HTML inside an AJAX object. – Idiot211 Oct 06 '15 at 09:28
  • @Idiot211 Can you take a screen shot of what's returned and post it here please – Izzy Oct 06 '15 at 09:28

0 Answers0