从js ajax中的jsp获取值并将其发送到POST

时间:2016-06-05 10:17:58

标签: javascript jquery ajax jsp

如何在不使用输入值和形式的情况下获取我的js中的值$ {tablet.tabletId}(存储在jsp中),以便将其发送到POST方法?我通过点击按钮跟踪js与订单控制器的交互:

/**
 * Created by User on 04.06.2016.
 */
$(document).ready(function(){
    $("#myButton").click(function(e){
        dataString = //somehow get ${tablet.tabletId}..
        $.ajax({
            type: "POST",
            url: "order",
            data: {tabletId: dataString},
            dataType: "json",

            success: function(parameter1) {
                if(data.success){
                    var footerId = "order-footer-" + parameter1;
                    var panelId = "panel-body-" + parameter1;
                    var href = "<a href='/patient/bucket' class='btn btn-success'>To Bucket</a>"
                    var input = "<input type='number' name='count' value='1'>" + "<h3>Added to <a href='/patient/bucket'>bucket</a> </h3>"
                    document.getElementById(footerId).innerHTML = href
                    document.getElementById(panelId).innerHTML = input
                }
                else {
                    $("#ajaxResponse").html("<div><b>Error!</b></div>");
                }
            },

            //If there was no resonse from the server
            error: function(jqXHR, textStatus, errorThrown){
                console.log("Something really bad happened " + textStatus);
                $("#ajaxResponse").html(jqXHR.responseText);
            },

            //capture the request before it was sent to server
            beforeSend: function(jqXHR, settings){
                //adding some Dummy data to the request
                settings.data += "&dummyData=whatever";
                //disable the button until we get the response
                $('#myButton').attr("disabled", true);
            },
            complete: function(jqXHR, textStatus){
                //enable the button
                $('#myButton').attr("disabled", false);
            }

        });
    });
});

0 个答案:

没有答案