URL在JSP中附加两次

时间:2017-10-17 03:20:12

标签: jquery jsp

您好我正在尝试使用Jquery在jsp页面中的按钮单击事件上传递URL。 我为此使用了ajax调用。 JSP:

<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
<a class="btn btn-success" style="width: 100%" id='approve'>Approve</a>
</div>

Jquery的

$(document).ready(function() {
            //LIVEICON
            $('#approve').on('click',function(){
                alert("Click");
                $('#formeffortloading').attr(
                        {                       
                            action : "${loginbean.contextPath}/effortloading/approveraction"
                        });
            var postData = $('#formeffortloading').serializeArray();
            var formURL = $('#formeffortloading').attr("action");
            console.log(':: Called :: '+formURL);
             $.ajax(
            {
                url : 'effortloading/approveraction',
                type: "POST",
                data : postData,
                success:function(data, textStatus, jqXHR) 
                {
                    //data: return data from server
                    console.log(':: << Success data >> :: '+data+':: textStatus ::'+textStatus+':: persistResponse ::'+data.persistResponse);

                },
                error: function(jqXHR, textStatus, errorThrown) 
                {
                        //console.log(':: << failed data >> :: '+data);
                        console.log(':: << failed data >> :: '+jqXHR+':: textStatus ::'+textStatus+':'+errorThrown);
                    //if fails      
                }
            }); 
                var comment = $('#comments').val();
                if(($('#comments').val()!="")){                             
                    //alert(comment);
                    $('#successalert').html('<strong>Approved...!</strong>').show().fadeOut(5000); 

                    }
                else{

                    if($("#comments").val()==""){$('#comments').css({'background-color':'rgba(255, 0, 0, 0.09)','border-color':'red'});$('#comments_er').css('display','block');}
                    //if($("#approver").val()==null){$('#approver').css({'background-color':'rgba(255, 0, 0, 0.09)','border-color':'red'});$('#approver_er').css('display','block');}
                }
            });
        });

但是当点击按钮时,URL会附加两次,例如 localhost:8081 / Costing / effortloading / effortloading / approveraction

请告诉我这是什么问题..

提前致谢..

2 个答案:

答案 0 :(得分:1)

此页面的当前网址似乎为localhost:8081/Costing/effortloading。这就是您在effortloading/approveraction中提供的相对网址ajax附加两次的原因。你可以做其中任何一个。

1)将ajax网址设为approveraction,以便effortloading不会被追加两次。

2)您可以使用JavaScript找到origin,然后添加effortloading/approveraction

例如:url: window.location.origin + 'effortloading/approveraction'

$.ajax({
  url: window.location.origin + 'effortloading/approveraction',
  type: "POST",
  data: postData,
  success: function (data, textStatus, jqXHR) {
    //data: return data from server
    console.log(':: << Success data >> :: ' + data + ':: textStatus ::' + textStatus + ':: persistResponse ::' + data.persistResponse);

  },
  error: function (jqXHR, textStatus, errorThrown) {
    //console.log(':: << failed data >> :: '+data);
    console.log(':: << failed data >> :: ' + jqXHR + ':: textStatus ::' + textStatus + ':' + errorThrown);
    //if fails      
  }
}); 

答案 1 :(得分:0)

您的帖子网址imageable_type是相对路径 您的jsp文件的路径可能是'effortloading/approveraction',因此您的ajax网址会变为localhost:8081/Costing/effortloading/

使用您的javascript的网站根路径或变量localhost:8081/Costing/effortloading/effortloading/approveraction

formURL