代理配置文件未读

我的开发环境angular服务器在localhost:4200上运行,而spring boot服务器在localhost:8080上运行。 Angular服务使用

调用rest api调用
this.http.get(window.location.protocol + '//' + window.location.hostname + ':8080/' +'<context-path>/api/<endpoint>');

代理配置文件:

{
    "/api": {
    "target": "http://localhost:800/","secure": false,"logLevel": "debug","changeOrigin": false
    }
}

我正在使用以下服务器启动服务器

ng serve --proxy-config proxy.conf.json

故意将端口号设置为800,但仍然可以从服务器获取结果。这确保了我不会读取代理配置文件。

当我们可以直接从角度服务访问URL时,为什么需要代理配置文件?

zhi686 回答:代理配置文件未读

使用代理文件基本上是告诉angular检查请求的url并将其替换为目标。
对于您的情况,请尝试请求String uid = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference uidRef = rootRef.child("businessowners").child(uid); ValueEventListener valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String txt_firstname = dataSnapshot.child("txt_firstname").getValue(String.class); String txt_lastname = dataSnapshot.child("txt_lastname").getValue(String.class); Log.d(TAG,txt_firstname + " " + txt_lastname); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { Log.d(TAG,databaseError.getMessage()); //Don't ignore errors! } }; uidRef.addListenerForSingleValueEvent(valueEventListener); ,您会看到对this.http.get('api/something')进行了重定向。

回答您的最后一个问题:
从本地服务直接访问url对于本地开发来说似乎很方便。但是,一旦您将后端设置在与前端应用程序不同的服务器上,就可以节省大量时间来试图弄清为什么会出现CORS错误。

本文链接:https://www.f2er.com/3143205.html

大家都在问