试图将值从Javascript传递到Java

时间:2018-01-29 04:12:40

标签: javascript java

我正在使用此Javascript从前端(Javascript)将x,y值发送到Java。

有时这段代码会将x任何y值传递给Java,有时则不会.②按下“保存”按钮后,将调用函数saveObjectMovingPath()

我不知道为什么它有时不起作用!任何人都可以帮我解决这个问题吗?

<script type="text/javascript">

function saveObjectMovingPath() {       

    var building_Id = $("#building").val();
    var floor_Id = $("#floor_level").val();

    console.log('building_Id :' + building_Id + ', floor_Id : ' + floor_Id);

    var json = {};
    json["building_Id"] = building_Id;
    json["floor_Id"] = floor_Id;
    json["dataset"] = dataset;                          

        $.ajax({ 
            type: "post",                                               

            url: "savepath?pathConfig=" + unescape(JSON.stringify(json)),


            success: function(data) {                                                                                         
                alert("response data : " + data);

                var response = JSON.parse(data);                                        
            } 
        }); 
    }

</script>

这是传递URL的示例数据:

"http://localhost:8080/Track/savepath?pathConfig={%22building_Id%22:%223%22,%22floor_Id%22:%2224%22,%22dataset%22:[{%22x%22:785.260498046875,%22y%22:624.4688720703125},{%22x%22:783.3544921875,%22y%22:624.4688720703125},{%22x%22:781.4485473632812,%22y%22:624.4688720703125},{%22x%22:779.5425415039062,%22y%22:624.4688720703125},{%22x%22:777.6365966796875,%22y%22:624.4688720703125},{%22x%22:775.7306518554688,%22y%22:624.4688720703125},{%22x%22:773.8246459960938,%22y%22:624.4688720703125},{%22x%22:770.9656982421875,%22y%22:624.4688720703125},{%22x%22:769.0596923828125,%22y%22:624.4688720703125},{%22x%22:767.1537475585938,%22y%22:624.4688720703125},{%22x%22:764.2947998046875,%22y%22:624.4688720703125},{%22x%22:763.341796875,%22y%22:624.4688720703125},{%22x%22:762.3887939453125,%22y%22:623.5159301757812},{%22x%22:760.4828491210938,%22y%22:623.5159301757812},{%22x%22:759.5298461914062,%22y%22:623.5159301757812},{%22x%22:757.6239013671875,%22y%22:623.5159301757812},{%22x%22:756.6708984375,%22y%22:623.5159301757812},{%22x%22:755.7178955078125,%22y%22:623.5159301757812},{%22x%22:754.7649536132812,%22y%22:623.5159301757812},{%22x%22:752.8589477539062,%22y%22:623.5159301757812},{%22x%22:751.9059448242188,%22y%22:623.5159301757812},{%22x%22:750.9530029296875,%22y%22:623.5159301757812},{%22x%22:749.0469970703125,%22y%22:623.5159301757812},{%22x%22:748.0940551757812,%22y%22:623.5159301757812},{%22x%22:747.1410522460938,%22y%22:624.4688720703125},{%22x%22:745.2350463867188,%22y%22:624.4688720703125},{%22x%22:744.2821044921875,%22y%22:625.421875},{%22x%22:743.3291015625,%22y%22:625.421875},{%22x%22:741.423095703125,%22y%22:625.421875},{%22x%22:740.4701538085938,%22y%22:626.3748779296875},{%22x%22:739.5171508789062,%22y%22:626.3748779296875},{%22x%22:738.5641479492188,%22y%22:626.3748779296875},{%22x%22:737.6112060546875,%22y%22:627.327880859375},{%22x%22:736.658203125,%22y%22:627.327880859375},{%22x%22:735.7052001953125,%22y%22:627.327880859375},{%22x%22:734.752197265625,%22y%22:627.327880859375},{%22x%22:733.7992553710938,%22y%22:628.2808227539062},{%22x%22:732.8462524414062,%22y%22:628.2808227539062}]}"

1 个答案:

答案 0 :(得分:0)

您尝试在POST方法

上发送GET参数

首先你需要在你的AJAX声明和console.log中调用错误函数,如果没有错误并且问题没有解决请检查你的java端(服务器端,也许你试图保存需要解析的东西)。 ..) 第二:使ajax语句看起来像这样:

    $.ajax({ 
        type: "post",                                                
        data : {
building_Id : $("#building").val(),
floor_Id : $("#floor_level").val()
dataset: dataset;  
                }
        url: "savepath?pathConfig=" + unescape(JSON.stringify(json)),....

...

而不是:

var json = {};
    json["building_Id"] = building_Id;
    json["floor_Id"] = floor_Id;
    json["dataset"] = dataset;