1. 程式人生 > >AJax請求處理成功卻不進入success的解決方案,(專案遷移至springboot中出現的問題)

AJax請求處理成功卻不進入success的解決方案,(專案遷移至springboot中出現的問題)

之前環境eclipse+ssm 執行一切ok

之後環境idea+maven+springboot 出錯

博主在出現這個問題的時候是在把ssm專案遷移成springboot中時出現的。

也就是說ssm專案中可以正常返回資料,springboot就不行了。

一般這種問題是controller返回的資料型別不符合ajax要求的資料型別導致!!

一般這種問題是controller返回的資料型別不符合ajax要求的資料型別導致!!

一般這種問題是controller返回的資料型別不符合ajax要求的資料型別導致!!

比如ajax需要的是文字型別,但是返回一個物件,那麼ajax就會往error走。

 不確定的可以在ajax中加入

error:function(){}

看看是否會執行function程式碼,當然裡面寫alert ()或console.log()都是可以的。

 

比如我在ssm框架下使用了fastjson,但是springboot中預設使用的是Jackson。

雖然我在springboot中匯入了fastjson的依賴!

但是我發現在springboot中整合fastjson是需要做其他的步驟的

 

1.把json改為test。

2.或乖乖做完整合fastjson的其他操作

參考其他部落格我運行了其中新增@bean註解的方式,親測可行!

package com.anyunpei;


import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@MapperScan("com.anyunpei.dao")
public class Application {
    @Bean
    public HttpMessageConverters fastJsonConfigure(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //日期格式化
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(converter);
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

 在springboot中的啟動類中新增@bean註解下的程式碼。注意加完這段程式碼後這裡需要新增很多依賴,idea中使用alt+enter匯入需要的依賴。

當然pom.xml中需要新增fastjson的依賴

        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>