1

I am trying to do a very simple task. Not sure what I am doing wrong. I need to pass some values to code behind method, do some calculations there and then return the result. I started with a test method. There are many examples on web. But nothing working for me.

Please see my default page code below:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication7._Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

  <script type="text/javascript">

      $(document).ready(function () {
          var loc = window.location.href;
          $("#btnClick").click(function (event) {
              $.ajax({
                  type: 'POST',
                  url: loc + "/GetMessage",
                  data: "{}",
                  contentType: "application/json; charset=utf-8"

               })
               .success(function (response) {
                    alert(response.d);

                })
                .error(function (response) {
                    alert(response.d);
                });
           });
      });

  </script>

   <input id="btnClick" type="button" value="button" />


</asp:Content>

The jquery has been referenced in master page:

<script src="Scripts/jquery-2.1.0.js"></script>
<script src="Scripts/jquery-2.1.0.min.js"></script>

codebehind:

    [WebMethod]
    public static string GetMessage()
    {
        return "test";
    }

I do get "Undefined" error. When I change the .success and .error function like below

           .success(function () {
                alert("S");

            })
            .error(function () {
                alert("E");
            });

It shows "S". My understanding is that it finds the code behind method. But the 'response' is undefined. Does it mean the code behind method is not returning the data in json format?

12
  • specify the dataType header for your return type. Commented Apr 28, 2014 at 11:47
  • Why are you loading jQuery twice? Use either jquery-2.1.0.js or jquery-2.1.0.min.js. I would recommend .min version Commented Apr 28, 2014 at 11:49
  • @Andrew Still getting the same error. Commented Apr 28, 2014 at 12:28
  • @Satpal Removed one reference, does not make any difference. Commented Apr 28, 2014 at 12:30
  • Check your ScriptManager. Set the "EnablePageMethods" property to true. Also, try setting return "test" to return "{\"test\":\"test\"}"; Commented Apr 28, 2014 at 12:31

5 Answers 5

1

I have finally resolved the issue by commenting out the code in App_Start. Murali's suggestion help me to find out the actual issue. The exception was 'authentication failed'. //settings.AutoRedirectMode = RedirectMode.Permanent;

Sign up to request clarification or add additional context in comments.

Comments

0

Please check this one.

       .success(function (data) {
            alert(data);

        })

Comments

0

First use only a single version(minified or unminified) of jquery and use dataType:'json' like,

$("#btnClick").click(function (event) {
    $.ajax({
        type: 'POST',
        url: loc + "/GetMessage",
        dataType: 'json', // for json data, which is parsed
        success:function (response) {//combining success and error callback in ajax
            console.log(response);
        },
        error:function (response) {
            console.log(response);
        }
   });
});

3 Comments

I am getting the same Error: [object Object].
This is because you are not responding your data as json, its simply a string, change this return "test"; to return '{"d":"test"}';
This will not run as valid c# code.I tried "{\"d\":\"test\"}"; All the examples I have seen on the web is exactly like this. Is there any major changes in recent JQuery versions?
0

Try removing .d and check with console

.success(function (response) {
  console.log(response); //  in firefox/chrome
  alert(response);

})

Check the firebug console for the JSON output. Below is sample screenshot

enter image description here

8 Comments

Still throwing exception. Only difference in format. It now shows [Object] [Object].
@Anup, check the firefox/chrome network/console and see what is the response from server
@Anup, I was asking you to check the response data , i.e json output. Not the headers
@Anup, check the updated answer for firebug console help
I cannot see jason in firebug. I have contentType: "application/json; in request. Could you please advise?
|
0

You´ll need to remove the attribute runat="server" in the following line:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

Because that says, that the javascript code runs at the server and not at the client/browser, as excepted. Changing this should fix all your problems.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.