search

Home  >  Q&A  >  body text

angular.js - Angular1+gulp 项目使用 http-proxy-middleware 作代理的问题

打包完成后,浏览器打开了该链接:http://localhost:3000/#/register
完成注册功能时有post请求,如注册信息的提交,有get请求,如检查手机号是否已注册。
现在遇到的问题是get请求都能正常使用,post请求都不能正常使用。

如下,get请求是正常的,拿到了期望的结果。

如下,post请求不正常,原本是post请求,怎么代理时变成了 Request Method:OPTIONS ,然后就出错了。

附上代理相关的配置:

var browserSync = require('browser-sync').create();
var proxyMiddleware = require('http-proxy-middleware');

gulp.task('server', ['build'], function() {
    var middleware = proxyMiddleware('/d', {target: 'http://api.b.cn', changeOrigin: true});
    browserSync.init({
        server: {
            baseDir:dist,
            index: 'index.html',
            middleware: middleware
        }
    });
});
天蓬老师天蓬老师2743 days ago874

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:16:17

    Thanks for the invitation!
    The cross-domain resource sharing standard adds a new set of HTTP header fields that allow the server to declare which origin sites have permission to access which resources. In addition, the specification requires that for those HTTP request methods that may have side effects on server data (especially HTTP requests other than GET, or POST requests with certain MIME types), the browser must first use the OPTIONS method to initiate a preflight request ( preflight request) to learn whether the server allows the cross-domain request. After the server confirms permission, it initiates the actual HTTP request. In the return of the preflight request, the server can also notify the client whether it needs to carry identity credentials (including cookies and HTTP authentication related data).
    It is recommended that you read the following article in detail:

    • HTTP access control

    To solve the problem, there are two ways:

    • Add CORS cross-domain request header

    • If you only need it for development, you can use Fiddler to set the CORS cross-domain request header. I recently discovered a Chrome artifact - CORS Toggle, which is very convenient.

    Reference resources

    • CORS Solution

    • Cross-domain problem, solution - CORS solution

    • Cross-origin resource sharing (CORS) you don’t know

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-15 17:16:17

    Have you solved it? I encountered the same problem

    reply
    0
  • Cancelreply