如何返回一个全局数组?

$(document).on("click", "#subtotal", function() {       
    var total_price=0;                                        
    for (i=1; i<= clicks; i++) {
        var element = parseFloat($("#total_price_"+i).html());
        total_price += parseFloat(element);
        var brand_code = $("#brand_"+i).val();
        var price = $("#price_"+i).html();
        var quantity = $("#quantity_"+i).html();
        if (element >=1 ) {
        total_price 
        $("#total").html(total_price);
            $.ajax({
                    type: "POST",
                    url: "modules/query_jquery/product_select.php",
                   data: {"new_invoice": "new_invoice", "brand_code": brand_code, "price" : price, "quantity" : quantity , "total_price" : total_price},
                   dataType: 'json',
                   success: function (data) {
                      message = data.message;
                      window.location.href = data.url;
                    }
            });

        }

    }
    alert(message);
});

i am trying to alert message but it's not defined ?? variable message that defined under the ajax jquery can not be defined ? !!!

Try executing the alert once the Ajax is complete:

  success: function (data) {
      message = data.message;
      window.location.href = data.url;
      test();
  }

  function test() {
      if (window.message) {
          alert(message);
      } else {
          alert("global message was not populated");
      }
  }

AJAX request are handeld asynchron!

When alert is called, the HTTP request hasn't returned yet.

Use

  • the async flag (http://api.jquery.com/jQuery.ajax/)
  • Deffer the call
  • Call the method from your success method

Example for deffering the ajax request:

        $(function () {
            var req1 = $.get('test1.json').success(function (data) {
                $('body').append(data.hallo);
            });
            var req2 = $.get('test2.json').success(function (data) {
                $('body').append(data.welt);
            });

            $.when(req1, req2).done(function () {
                $('body').append('<br />ajax completed');
            }).fail(function () {
                $('body').append('<br />ajax failed');
            });
        });

As mentioned, because you're defining your global var inside a ajax success function it means that if this isn't successful this var won't exist for you to call on.

So define message before the ajax call to be on the safe side:

message = null;

$(document).on("click", "#subtotal", function() {       
var total_price=0;                                        
for (i=1; i<= clicks; i++) {
    var element = parseFloat($("#total_price_"+i).html());
    total_price += parseFloat(element);
    var brand_code = $("#brand_"+i).val();
    var price = $("#price_"+i).html();
    var quantity = $("#quantity_"+i).html();
    if (element >=1 ) {
    total_price 
    $("#total").html(total_price);
        $.ajax({
                type: "POST",
                url: "modules/query_jquery/product_select.php",
               data: {"new_invoice": "new_invoice", "brand_code": brand_code, "price" : price, "quantity" : quantity , "total_price" : total_price},
               dataType: 'json',
               success: function (data) {
                  message = data.message;
                  window.location.href = data.url;
                }
        });

    }

}
alert(message);
});