Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法将表单参数发送到服务器_Javascript_Jquery_Spring Mvc - Fatal编程技术网

Javascript 无法将表单参数发送到服务器

Javascript 无法将表单参数发送到服务器,javascript,jquery,spring-mvc,Javascript,Jquery,Spring Mvc,我们遇到一个问题,通过javascript提交表单时,其中一个参数(invoiceCodes)没有发送到服务器。下面是javascript代码的片段 流程如下。当用户单击“打印”按钮时,将调用validateTransition()方法,我们在该方法中进行ajax调用。在ajax响应之后,我们调用couponPopup(url,invoiceCodes)。在此函数中,我们提交newWinForm,但有时invoiceCodes参数被发送为空 在需要用户输入发票代码的情况下,checkForInv

我们遇到一个问题,通过javascript提交表单时,其中一个参数(
invoiceCodes
)没有发送到服务器。下面是javascript代码的片段

流程如下。当用户单击“打印”按钮时,将调用validateTransition()方法,我们在该方法中进行ajax调用。在ajax响应之后,我们调用
couponPopup(url,invoiceCodes)
。在此函数中,我们提交
newWinForm
,但有时
invoiceCodes
参数被发送为空

在需要用户输入发票代码的情况下,
checkForInvoiceCode
也是正确的

我们在表单中输入值的方式是否有任何错误,可能导致
invoiceCodes
有时无法发送

function couponPopup(url, invoiceCodes)
{
    var selectedOrders = '';
    $(".selectedOrder:checked").each(function() {
        selectedOrders += $(this).val() + ',';
    });

    var frm = document.forms["newWinForm"];
    frm.action = url;
    frm.selectedShipments.value= selectedOrders; 
    frm.invoiceCodes.value = invoiceCodes; 
    console.log("Selected orders are "+selectedOrders);
    console.log("Invoice codes with them in order are "+invoiceCodes);
    document.getElementById("hiddenInvoiceCodes").value=invoiceCodes;
    document.getElementById("hiddenselectedShipments").value=selectedOrders;
    frm.submit();
    return false;
}

function validateTransition() {
    $('#statusChangeSuccess').hide();
    $('#statusChangeFail').hide();

    var selectedOrders = '';
    var invoiceCodes = '';
    var flag = 0;
    var spaceError = 0;
    var commaError = 0;

    $(".selectedOrder:checked").each(function() {
        selectedOrders += $(this).val() + ',';
        <c:if test="${checkForInvoiceCode}">
        var emptyPattern = /^\s*$/;
        var commaPattern = /,/;
        var inv_code = $("#invoice-code-" + $(this).val()).val().trim();
        if (emptyPattern.test(inv_code)) {
            spaceError = 1;
            flag = 1;

        }
        if (commaPattern.test(inv_code)) {
            commaError = 1;
            flag = 1;

        }
        invoiceCodes += inv_code + ",";
        </c:if>
    });

    if(selectedOrders=='') {
        alert('Please select at least one order');
        return false;
    }

    if ( flag ) {
        if ( commaError ) {
            alert('One or more specified codes have comma, please remove comma from them');
        }
        if ( spaceError ) {
            alert('One or more specified codes has been left blank, please fill them up');
        }
        if ( !commaError && !spaceError ) {
            alert('Please contact tech');
        }

        return false;
    }

    var inputdata = {"selectedShipments" : selectedOrders,
        "statusCode" : "PRINT"
    };

    //this is where we are making an ajax call 
    jQuery(function($){
            setTimeout(function(){                      
                var ajaxUrl = '/product/update/';

                $.ajax({url:ajaxUrl, type: "POST", dataType: 'json', data:inputdata , success: function(data) {
                    if(data['status'] == 'success') {
                        //couponPopup function is called where form is submitted
                        couponPopup("${path.http}/product/print/", invoiceCodes);
                        $('#statusChangeSuccess').html(data['message']).show();

                        $(".selectedOrder:checked").each(function() {
                            $("#row-" + $(this).val()).remove();
                        });

                    } else{
                        $('#statusChangeFail').html(data['message']).show();
                    }
                }});
        }, 10 );
    });

    return false;
}

<form id="newWinForm" name="newWinForm" action="" method="post" target="_blank" >
    <input type="hidden" id="hiddenselectedShipments" name="selectedShipments" value="" />
    <input type="hidden" id="hiddenInvoiceCodes" name="invoiceCodes" value="" />
</form>

有时不发送,有时是否会获取值使用微调功能的方式可能会导致错误,从而阻止正确发布值。请阅读文档使用的是$.trim(str),而不是字符串字段上的.trim()。不知道它是否与您的问题有关,但知道您的jQuery和常规javascript的混合。使用一个或另一个来避免维护噩梦为什么要设置超时包装ajax?@franchez如果是问题,则警报必须一直存在,然后inv_代码将为空
@RequestMapping("/product/print")
public void printSelectedPendingOrders(@RequestParam("selectedShipments") String selectedShipments,
        @RequestParam(defaultValue = "", value = "invoiceCodes", required = false) String invoiceCodes, ModelMap modelMap, HttpServletResponse httpResponse)
        throws IOException, DocumentException, ParserConfigurationException, SAXException {