Js + Jquery 积累

版权声明:本文为博主原创文章,如有错误劳烦指正。转载请声明出处,便于读取最新内容。——Bestcxx https://blog.csdn.net/bestcxx/article/details/83819232

js 获取全局路径

直接在单独到 js 文件中是无法获取上下文路径的,需要先单独在jsp页面设置一下,然后再在js 文件中直接使用

在 jsp 页面声明 javascript 片段
<script type="text/javascript">
    var contextPath = "${pageContext.request.contextPath}";
</script>
在 jsp 页面使用 jstl标签
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
在 jsp 页面声明 java 代码
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
js 操作 Json
字符转 JSON 对象 并读取
var str1 = '{ "name": "123", "age": 11 }';//JSON字符串
var obj1 = JSON.parse(str1); //由JSON字符串转换为JSON对象 
console.log(obj1.name);//打印 JSON 对象的name属性值
JSON 对象 转 字符串
var str2=JSON.stringify(obj1);//将 JSON 对象转化为 字符串
console.log(str2);//打印字符串到控制台

猜你喜欢

转载自blog.csdn.net/bestcxx/article/details/83819232