1

I write a script that call a method which return count of some data, and then change the text of a 'P' tag (by id=count) to returned value. my script :

$(document).ready(function () {
    $("#count").text(function () {
        $.ajax({
            type: "POST",
            url: "./WebForm1.aspx/GetCountUnCheckNotification",
            data: {},
            async: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                return (response.d);
            },
        });
    });
});

What is the problem?

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
pmn
  • 2,176
  • 6
  • 29
  • 56

7 Answers7

2

If your response is a json,then you need to parse it like below.And if your response is not in json,you can directly assign value to .text(response)

          $(document).ready(function () {

                     $.ajax({
                        type: "POST",
                        url: "./WebForm1.aspx/GetCountUnCheckNotification",
                        data: {},
                        async: false,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success:
                            function (response) {
                                var result=parseJSON(response)
                                $("#count").text(result.d)
                            },
                    });

                });
          });
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
1

You have a bug relating to how AJAX works - your function isn't returning anything. Your success callback is invoked after the function has returned because AJAX is asynchronous.

To make this work, you would have to start with the AJAX call, and then, in it success handler, set the text of the <p>:

$.ajax({
  ...
  success: function (result) {
    $("#count").text(result.d);
  }
})
user229044
  • 232,980
  • 40
  • 330
  • 338
1

try:

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "./WebForm1.aspx/GetCountUnCheckNotification",
        data: {},
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            $("#count").text(response.d);
        },
    });
});
M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76
0
$.ajax({
                        type: "POST",
                        url: "./WebForm1.aspx/GetCountUnCheckNotification",
                        data: {},
                        async: false,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success:
                            function (response) {
                               $("#count").val(response["id"]); // in case of input tag
                              $("#count").html(response["id"]); // in case of span/p/div
                            },
                    });
jaipster
  • 11,967
  • 2
  • 21
  • 24
0

You need to set $("#count").text() in the success callback...

$(document).ready(function () {
                    $.ajax({
                        type: "POST",
                        url: "./WebForm1.aspx/GetCountUnCheckNotification",
                        data: {},
                        async: false,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success:
                            function (response) {
                                $("#count").text(response.d);
                            }
                    });
                }
Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
0

The $.ajax function doesn't return anything. Try something like this:

 $.ajax({
    type: "POST",
    url: "./WebForm1.aspx/GetCountUnCheckNotification",
    data: {},
    async: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success:
        function (response) {
            $("#count").text(response.d);
        },
});
crunch
  • 675
  • 3
  • 8
0

It doesn't work because of the way asynchronous operations work. Basically you are telling $('#count').text() to immediately set the text to whatever the anonymous function returns. But that function will just return undefined after firing the ajax event. You must place the text call inside the final callback; which is not executed until later:

$(document).ready(function () {
  $.ajax({
    type: "POST",
    url: "./WebForm1.aspx/GetCountUnCheckNotification",
    data: {},
    async: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success:
    function (response) {
      $("#count").text(response.d);
    },
  });
});
Plato
  • 10,812
  • 2
  • 41
  • 61