Javascript 为什么我的text()函数在脚本中不起作用?

Javascript 为什么我的text()函数在脚本中不起作用?,javascript,jquery,Javascript,Jquery,我编写了一个脚本来调用一个方法,该方法返回一些数据的计数,然后通过id=count将“p”标记的文本更改为返回值。 我的剧本: $(document).ready(function () { $("#count").text(function () { $.ajax({ type: "POST", url: "./WebForm1.aspx/GetCountUnCheckNotification",

我编写了一个脚本来调用一个方法,该方法返回一些数据的计数,然后通过id=count将“p”标记的文本更改为返回值。 我的剧本:

$(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);
            },
        });
    });
});

问题出在哪里?

您有一个与AJAX工作方式相关的bug—您的函数没有返回任何内容。您的成功回调在函数返回后被调用,因为AJAX是异步的

要实现这一点,您必须从AJAX调用开始,然后在it成功处理程序中设置以下内容的文本:


您有一个与AJAX工作方式相关的bug—您的函数没有返回任何内容。您的成功回调在函数返回后被调用,因为AJAX是异步的

要实现这一点,您必须从AJAX调用开始,然后在it成功处理程序中设置以下内容的文本:


若您的响应是json,那个么您需要像下面那个样解析它。若您的响应不是json,那个么您可以直接为.textresponse赋值


若您的响应是json,那个么您需要像下面那个样解析它。若您的响应不是json,那个么您可以直接为.textresponse赋值

您需要在成功回调中设置$count.text

$(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);
                            }
                    });
                }
您需要在成功回调中设置$count.text

$(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);
                            }
                    });
                }
尝试:

尝试:


$.ajax函数不返回任何内容。试着这样做:

 $.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);
        },
});

$.ajax函数不返回任何内容。试着这样做:

 $.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);
        },
});

由于异步操作的工作方式,它无法工作。基本上,您是告诉$'count'.text立即将文本设置为匿名函数返回的任何值。但是,在触发ajax事件后,该函数将返回undefined。您必须将文本调用放在最终回调中;直到稍后才会执行:

$(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);
    },
  });
});

由于异步操作的工作方式,它无法工作。基本上,您是告诉$'count'.text立即将文本设置为匿名函数返回的任何值。但是,在触发ajax事件后,该函数将返回undefined。您必须将文本调用放在最终回调中;直到稍后才会执行:

$(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);
    },
  });
});
同样成功:函数响应{return response.d;},删除尾部逗号同样成功:函数响应{return response.d;},删除尾部逗号
$(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);
    },
  });
});