angular ajax

  在使用angular 发送ajax的时候,状态信息是正常的,状态码200,返回的参数是使用@responsebody转换后返回的字串。在前端却总是在调用错误的回调函数,也拿不到正确的反馈信息。

  回调函数中传回的参数是:

  Error: [$http:baddata] http://errors.angularjs.org/1.7.2/$http/baddata?

  在浏览器端调试会出现:

  Error: json.parse: unexpected character at line 1 column 1 of the json data

  这时可能是在springmvc的配置文件中,没有配置json消息转换器,

  

<!-- Start: 配置json消息转换器 & 参数解析-->
    <bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
        <property name="dateFormat">
            <bean class="java.text.SimpleDateFormat">
                <constructor-arg index="0" type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
            </bean>
        </property>
    </bean>
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json; charset=UTF-8</value>
                    </list>
                </property>
                <property name="prettyPrint" value="true"/>
                <property name="objectMapper" ref="objectMapper"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- End: 配置json消息转换器 & 参数解析 -->

  当然也要先导入这些依赖先。

---不要装作很努力
原文地址:https://www.cnblogs.com/goblinn/p/9343079.html