我不懂AJAX回调

时间:2013-03-15 15:25:06

标签: javascript ajax jquery

我有一个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;
    ?>

警报不会弹出。任何人都知道为什么???

3 个答案:

答案 0 :(得分:2)

我使用其他网址尝试了您的代码并且运行良好。 有三种情况:

  • scriptUrl计算不正确,并未指向您的PHP脚本
  • 您的服务器已关闭
  • 您正在访问未在与您的脚本相同的域下提供的网址(同源政策)

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:-1)

试试这个

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