繁体   English   中英

我不懂AJAX回调

[英]I don't understand AJAX callbacks

我有一个javascript函数,它在更改下拉列表时执行:

    <script type="text/javascript">
        $(function()
        {
            // Executes when the status dropdown changes value
            $('select[name="status_dropdown"]').change(function(event)
            {
                var $this = $(event.target);
                var orderId = $this.closest('tr').children('td:eq(0)').text(); // index 0 refers to the "order_id column" in the table

                var result = null;
                var scriptUrl = "ajax_php/update_status.php?order_id=" + orderId + "&status_id=" + this.value;

                $.ajax(
                {
                    url: scriptUrl,
                    type: 'get',
                    dataType: 'html',
                    async: false,
                    success: function(data)
                    {
                       result = data;
                       alert(result);
                   }
                });
            });
        })
    </script>

我正在尝试获取警报调用以显示以下php代码的返回值(这是真的):

    <?php
        .
        .
        .

        return true;
    ?>

警报不会弹出。 谁知道为什么???

我用另一个URL尝试了你的代码,它运行良好。 有三种情况:

  • scriptUrl计算不正确,并未指向您的PHP脚本
  • 你的服务器坏了
  • 您正在访问未在与您的脚本相同的域下提供的URL(同源策略)

如果向ajax参数添加错误处理程序,则可以查看错误的详细信息:

error : function(jqXHR, textStatus, errorThrown) {
    alert(errorThrown);
}

返回仅返回php脚本中的值 - 将其输出到ajax,您需要将结果实际输出到页面,在这种情况下类似echo "true"; print("true");

尝试这个

$(document).ready(function(){
    $('select[name="status_dropdown"]').change(function(event)
        {
            var $this = $(event.target);
            var orderId = $this.closest('tr').children('td:eq(0)').text(); // index 0 refers to the "order_id column" in the table

            var result = null;
            var scriptUrl = "ajax_php/update_status.php?order_id=" + orderId + "&status_id=" + this.value;

            $.ajax(
            {
                url: scriptUrl,
                type: 'get',
                dataType: 'html',
                async: false,
                success: function(data)
                {
                   result = data;
                   alert(result);
               }
            });
        });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM