2

I searched and tried for days now but I cant find any solution. I looked up nearly every tutorial and nearly every question on this board, but no code worked for me. (I know this is a duplicate question but since no other code worked at all, you are my last hope)

I'm trying to return a AJAX request from C# in ASP but no matter what it always returns undefinded even with code from tutorials.

Here is what I have in my Default.aspx

<script type="text/javascript">
    $(document).ready(function () {

     $.ajax({
         type: "POST",
         url: "Default.aspx/GetData",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (response) {
             alert(response.d);
         },
         failure: function (response) {
             alert(response.d);
         }
     });
 });
</script>

and here is my codebehind from Default.aspx.cs

[WebMethod]
public static string GetData()
{
    return "This string is from Code behind";
}

The Problem is: the c# method dont activate... I dont know why and I get no errormessage.

Can you pls help me? Thanks for your advice

M. Schena
  • 2,039
  • 1
  • 21
  • 29
Robin Schambach
  • 617
  • 3
  • 11
  • 26

3 Answers3

3

I just copy pasted your code and it worked Like a charm as one can see in the following links

http://prntscr.com/baw6qb

I don't see what are you doing wrong, Please place a debugger in you JAVASCRIPT and see if its been called or Not. Rest is working fine in my Environment. There is no mismatch of text or no json instead of string needed. Please check you code thoroughly. you must be missing something some where
Output screenshot enter link description here

Update

enter image description here

KhawajaAtteeq
  • 401
  • 3
  • 6
  • your links are blocked by my companies filter^^" I cant see your screenshot or code My Javascript is called and workes, but my c# code dont run. No matter what I code in my c# function it does not run. Is there any trick or hidden hint that I have to do in Visual studio? – Robin Schambach Jun 01 '16 at 06:44
  • as I said my dear friend I just copy past your code and it worked as you can't see the picture, let me tell you I'd a debugger placed their, and it was working. if its not calling your method in any case. can you use `firebug` to see if the request was even sent.`Net` option of the `firebug` shows you all the `methods` that execute from your page. please check that once – KhawajaAtteeq Jun 01 '16 at 06:50
  • Firebug Message from Default.aspx/GetData: {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException" } – Robin Schambach Jun 01 '16 at 06:56
  • Now that is your actual error `Authentication failed` – KhawajaAtteeq Jun 01 '16 at 07:00
  • This means `User authentication hasn't been provided` or It failed some how` – KhawajaAtteeq Jun 01 '16 at 07:01
  • do you've access to the page, what I mean to say is are you `authorized` user to the page – KhawajaAtteeq Jun 01 '16 at 07:02
  • Well I just created a new ASP.NET web project, i have not changed any permissions at all. btw thank you very much for your help :D I've tried it with this http://stackoverflow.com/questions/29911785/authentication-failed-error-when-calling-web-method-using-ajax solution but I got "Server Error in '/' Application. Object reference not set to an instance of an object. " with that – Robin Schambach Jun 01 '16 at 07:10
  • `Object reference not set to an instance of an object` means you are sending something null and it can't take a `null` value – KhawajaAtteeq Jun 01 '16 at 07:14
  • ahh i got it now.. I just added ` ` in my Web.config and it works now – Robin Schambach Jun 01 '16 at 07:20
  • glad to know that, `remember` always debug your `javascript` – KhawajaAtteeq Jun 01 '16 at 07:26
1

Build format like this

{
    "employees":[
                  {"firstName":"John", "lastName":"Doe"},
                  {"firstName":"Anna", "lastName":"Smith"},
                  {"firstName":"Peter", "lastName":"Jones"}
    ]
}

and access through in jquery like employees[0].firstname

Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
1

I had the exact same problem and it was driving me crazy. Finally, after hours of doing this and that, I checked the JSON response and it was like this : {"Message":"Authentication failed.", "StackTrace":null, "ExceptionType":"System.InvalidOperationException"}

And I searched for this issue and FINALLY found an answer over here! Hope this helps you too! :)

Community
  • 1
  • 1
Prabash Darshana
  • 985
  • 2
  • 9
  • 17