【问题标题】:Post JS data to page B将JS数据发布到页面B
【发布时间】:2013-12-15 19:16:34
【问题描述】:

我用这块JS:

$.get( "/upload/number.php", function( data ) {
        alert( "Data Loaded: " + data );
        });

这会调用 number.php,它会返回一个数字。在上面的脚本中,我提醒数据检查这是否有效,并且确实有效。在警报中,我看到了数字。

现在我想把这个号码发送到另一个页面,我试过这样:

$('#file_upload').data('uploadifive').settings.formData = {
    'number'      : +data
};

这不起作用,萤火虫说:

SyntaxError: missing } after property list


'number' : data

我怎样才能做到这一点?

完整的脚本:

<script type="text/javascript">
//<![CDATA[
$(function() {
    // Initialiseer uploadifive
    $('#file_upload').uploadifive({
        'auto'              : false,
        'checkScript'       : '/uploadifive/Sample/check-exists.php',
        'onFallback'        : function () {
                                  window.location = '/home.php';
                              },
        'method'            : 'POST',
        'queueID'           : 'queue',
        'uploadScript'      : '/upload/uploadifive.php',
        'removeCompleted' : true,
        'onUploadComplete'  : function (file, data) {
                                  console.log(data);
                              }
    });
     // Hang een click-event aan de knop
    $('#subby').on('click', function () {

    $.get( "/upload/number.php", function( data ) {

        alert( "Data Loaded: " + data );

        });


        // Werk formData van uploadifive bij met de betreffende waarden
        $('#file_upload').data('uploadifive').settings.formData = {

            'number'      : data

        };
        // Voer de upload uit
        $('#file_upload').uploadifive('upload');
    });
});
//]]>
</script>

【问题讨论】:

    标签: javascript php jquery


    【解决方案1】:

    对于您的特定问题(合并数组),您有几个问题。首先是范围,因为data 不是全局的。然后您可以merge the data 或将变量的范围设置得更高:

    <script type="text/javascript">
    //<![CDATA[
    $(function() {
        // Initialiseer uploadifive
        $('#file_upload').uploadifive({
            'auto'              : false,
            'checkScript'       : '/uploadifive/Sample/check-exists.php',
            'onFallback'        : function () {
                                      window.location = '/home.php';
                                  },
            'method'            : 'POST',
            'queueID'           : 'queue',
            'uploadScript'      : '/upload/uploadifive.php',
            'removeCompleted' : true,
            'onUploadComplete'  : function (file, data) {
                                      console.log(data);
                                  }
        });
         // Hang een click-event aan de knop
        $('#subby').on('click', function () {   
            $.get( "/upload/number.php", function( returnednumber ) {
                // Werk formData van uploadifive bij met de betreffende waarden
                $('#file_upload').data('uploadifive').settings.formData = {  
                    'number': returnednumber
                    };
                });
    
                // Voer de upload uit
                $('#file_upload').uploadifive('upload');
            });
        });
    //]]>
    </script>
    

    【讨论】:

    • 在您的示例中,我将使用 $.post("/url/to/post", $.extend({}, formdata, number)); 发布数据但是 Uploadifive 确实已经在 $('#file_upload').uploadifive('upload'); 之后发送了数据。还有其他选择吗?
    • 我并没有完全理解你的代码,我看不到它再次发布在哪里。但是,是的,还有另一种方法,如果我没记错的话,您可以在代码中将变量设置得更高并分配它。更新了代码。
    • 好吧,这行得通,只有一个问题:每次点击上传按钮时,var returnednumber 中的数字都必须改变。现在,var 只填充了一次代码。我该如何改变这个?
    • 好的,现在试试。我将uploadifive() 设置为$.get() 的回调。这意味着它只会在收到请求后执行。之前的问题是它同时发送了两个请求,所以第二个请求的值是第一个请求的旧值。
    猜你喜欢
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    相关资源
    最近更新 更多