Jquery 中截取项目路径,用于给ajax的url路径赋值
<script src="static/jquery-3.3.1.js"></script>
<script type="text/javascript">
$(function(){
$("#submitLogin").click(function(){
var name = $("#name").val();
var pwd = $("#pwd").val();
var users = {name:name,pwd:pwd}; //拼装成json格式
/* js中获取项目的路径 */
//方法一
var protocol = window.location.protocol;
var host = window.location.host;
var pathname = window.location.pathname;
var projectName = pathname.substring(0,pathname.substr(1).indexOf('/')+1);
//方法2
var curPath = window.document.location.href
var projectNameall = curPath.substring(0,find(curPath,'/',3));
$.ajax({
type:"POST",
/* url: protocol + "//" + host + projectName +"/addUser", */
url: projectNameall + "/addUser",
data:users,
success:function(data){
alert("成功");
},
error:function(e) {
alert("出错:"+e);
}
});
});
/* 查询str字符中cha字符在第 num 次出现的位置 */
function find(str,cha,num){
var x=str.indexOf(cha);
for(var i=0;i<num;i++){
x=str.indexOf(cha,x+1);
}
return x;
}
});
</script>