0

I have a javascript function which executes on the change of a dropdown:

    <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>

I am trying to get the alert call to show the return value of the following php code (which is true):

    <?php
        .
        .
        .

        return true;
    ?>

The alert doesn't pop up. Anyone know why ???

4

3 回答 3

2

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

  • scriptUrl 未正确计算且未指向您的 PHP 脚本
  • 您的服务器已关闭
  • 您正在访问的 URL 与您的脚本之一不在同一域下提供(同源策略)

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

error : function(jqXHR, textStatus, errorThrown) {
    alert(errorThrown);
}
于 2013-03-15T15:55:52.650 回答
1

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

于 2013-03-15T15:26:40.817 回答
-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);
               }
            });
        });
});
于 2013-03-15T15:50:33.030 回答