Ant Mobile使用整理

  1. fetch 请求java后台接收不到参数解决如下:
    Ant Mobile使用整理_第1张图片
  • 很明显axios请求的Content-Type为application-json,所以后台接收不到

    解决:参数使用第三方工具qs.stringify(param) 处理,然后Content-Type设置为 application/x-www-form-urlencoded, 注意,Request Payload 和 Form Data 的区别,主要是参数放的不对导致获取不到
    参考 参考2 参考3 参考4
  1. React------The “injectBabelPlugin” helper has been deprecated as of v2.0. 报错

使用Antd,会有报错

引入 react-app-rewired 并修改 package.json 里的启动配置。由于新的 [email protected] 版本的关系,你还需要安装 customize-cra,否则会出现如下的问题。

The "injectBabelPlugin" helper has been deprecated as of v2.0. You can use customize-cra plugins in replacement - https://github.com/
arackaf/customize-cra#available-plugins
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `react-app-rewired start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
 
npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2019-04-25T06_55_17_287Z-debug.log

解决如下:

  • 确保安装’customize-cra’并[email protected]
  • 确保安装’less’和’less-loader’
  • src目录新建 config-overrides.js 文件
    内容如下:
const {
    override,
    fixBabelImports,
    addLessLoader,
  } = require("customize-cra");
  
  
  module.exports = override(
    fixBabelImports("import", {
      libraryName: "antd", libraryDirectory: "es", style: true // change importing css to less
    }),
    addLessLoader({
      javascriptEnabled: true,
      modifyVars: { "@primary-color": "#1DA57A" }
    })
  );

记得关闭项目,重新启动
参考 参考

  1. react中配置代理解决跨域问题
    项目中遇到请求跨域问题,我们一般需要项目中配置代理解决。
    我的项目是create-react-app2.0版本生成的。
    首先安装包 http-proxy-middleware
npm install http-proxy-middleware --save
# or
yarn add http-proxy-middleware

然后在src目录下创建 setupProxy.js 文件
最后设置代理

// setupProxy.js
const proxy = require('http-proxy-middleware')

module.exports = function(app) {
  app.use(
    proxy('/api', {  //`api`是需要转发的请求 
      target: 'http://localhost:5000',  // 这里是接口服务器地址
      changeOrigin: true,
    })
  )
}

这里需要注意的是我们的axios的baseURL,设置的时候不能还设置加上原来服务器域名的地址,应该只设置 /api 就好了

axios.defaults.baseURL = '/api';
例如:
axios.post('/v1/register');

发布的时候可以使用nignx转发解决跨域,测试也可以


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8048;
        server_name  localhost;  

        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET,POST';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';  

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            root   html/build;
            index  index.html index.htm;
            proxy_pass  http://60.64.5.51:8038;     #请求转向mysvr 定义的服务器列表  
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # 代理接口地址配置
        location ^~/cfcExceptionEvent/v1/ {
            proxy_pass   http://111.231.97.228:10002;
			proxy_set_header   X-Forwarded-Proto $scheme;
			proxy_set_header   Host              $http_host;
			proxy_set_header   X-Real-IP         $remote_addr;		
		#	rewrite ^/cfcExceptionEvent/v1/(.*)$ /$1 break;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


参考 参考2 参考3 参考4

你可能感兴趣的:(react)