美文网首页
springMVC4.X json 跨域问题

springMVC4.X json 跨域问题

作者: 好好学习天天引体向上 | 来源:发表于2017-01-17 18:39 被阅读0次

首先,关于迁移至spring4.x需要发生的配置改动

  • json依赖包发生变化,maven中获取
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.5.0</version>
    </dependency>
  • 配置文件需要修改
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
  • 前端传回数据
$.ajax({
                    url: 'url',
                    method: 'POST',
                    dataType: 'JSON',
                    contentType:'application/json;charset=UTF-8',
                    data: JSON.stringify(req_data),
                    success: function () {
                    }
                });

解决跨域问题

  • 全局配置方式

spring servletContext配置文件中添加

<!-- API 接口跨域配置 -->
    <mvc:cors>
        <mvc:mapping path="/**" 
                     allowed-origins="*"
                     allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
                     allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
                     allow-credentials="true" />
    </mvc:cors>

相关文章

  • springMVC4.X json 跨域问题

    首先,关于迁移至spring4.x需要发生的配置改动 json依赖包发生变化,maven中获取 配置文件需要修改 ...

  • HTML-获取iframe元素的正确方法

    跨域相关文章详解js跨域问题JavaScript跨域总结与解决办法 解释最清楚的jsonpWhat is JSON...

  • 跨域

    博客 说说跨域那些事儿 不要再问我跨域的问题了 前端常见跨域解决方案(全) 同源策略 JSONP(填充式JSON)...

  • 理解跨域异步请求的 JSON-P

    在开篇之前,我们也许知道跨域问题的存在,知道通过服务端开放跨域请求来使API实现跨域访问,甚至也知道JSON-P这...

  • 3.解决uni-app开发环境中H5端跨域问题

    此跨域问题只存在于浏览器端,App和小程序不存在跨域问题 manifest.json官方配置文档: https:/...

  • 造成 Provisional headers are shown

    跨域使用了json

  • JWT简记

    JSON Web Token(JWT)是目前最流行的跨域认证解决方案。 跨域认证问题及传统解决方案 传统认证流程 ...

  • Ajax请求跨域问题

    遇到ajax请求跨域问题,解决方式,改dataType为jsonp json和jsonp返回数据格式json格式 ...

  • 前端必备HTTP技能之JSONP技术详解

    JSONP(JSON with Padding)是一种JSON扩展,用来解决由浏览器同源策略引起跨域限制。跨域限制...

  • jsonp

    用途 主要用来解决api使用的跨域问题JSONP(JSON with Padding)是JSON的一种“使用模式”...

网友评论

      本文标题:springMVC4.X json 跨域问题

      本文链接:https://www.haomeiwen.com/subject/gyzmbttx.html