构建 REST angular 应用程序的正确方法是什么?
What is correct approach to build REST angular application?
我有一些 java 申请。它为 REST 客户端提供 json 并使用 localhost:8080 来处理请求。
我还有 angular 2 应用程序,它使用 localhost:3000 来工作(我假设我可以将 typescript 文件编译成 js 并在没有 lite-server 的情况下使用,但不是:
zone.js@0.6.25?main=browser:269 Uncaught Error: More tasks executed then were scheduled.
index.html:20 Error: Error: XHR error loading file:///C:/dev/angular2app/app/main.js(…)
)。我的 angular 服务正在尝试从某些 url 获取 json 表单服务器,如下所示:
http://localhost:8080/api/test
当然,我收到有关禁止跨域请求的错误。但是,如果我在与 java 应用程序相同的端口上启动 angular 应用程序,其中之一将没有位置(端口繁忙等)。
请问,谁能解释构建此类应用程序的正确架构方法?
在你的情况下,AngularJS 应用程序,即 JS,HTML 文件等需要打包成一个 WAR 文件(我假设它是一个 WAR 文件,因为您使用的是 Tomcat 8080),否则下面的概念也会成立。一旦将应用程序部署到容器上(假设容器是 运行 on 8080),您将请求应用程序可能 URL http://IP:8080/appName/. In this case, there is no conflict of ports (actually conflict scenario does not apply). You have mentioned your angular works on port 3000, by which I assume you have hard coded the service URL as (probably) http://localhost:3000/. Now this would not work as the server is running on 8080. What you need to do is relativise the service invocation URLs in your application or hard code http://localhost:8080/ for your service URLs in Angular application. There is also another consideration, if you are using IP to access the application with browser and you are using hardcoded localhost in your service invocation URL, then the cross-domain problem would come. To avaid this use the URL as http://localhost:8080 ... 从浏览器。但是,如果您在 Angular 应用程序中相对化服务调用 URL,那么您可以使用浏览器中的 IP 或本地主机。
我有一些 java 申请。它为 REST 客户端提供 json 并使用 localhost:8080 来处理请求。
我还有 angular 2 应用程序,它使用 localhost:3000 来工作(我假设我可以将 typescript 文件编译成 js 并在没有 lite-server 的情况下使用,但不是:
zone.js@0.6.25?main=browser:269 Uncaught Error: More tasks executed then were scheduled.
index.html:20 Error: Error: XHR error loading file:///C:/dev/angular2app/app/main.js(…)
)。我的 angular 服务正在尝试从某些 url 获取 json 表单服务器,如下所示:
http://localhost:8080/api/test
当然,我收到有关禁止跨域请求的错误。但是,如果我在与 java 应用程序相同的端口上启动 angular 应用程序,其中之一将没有位置(端口繁忙等)。
请问,谁能解释构建此类应用程序的正确架构方法?
在你的情况下,AngularJS 应用程序,即 JS,HTML 文件等需要打包成一个 WAR 文件(我假设它是一个 WAR 文件,因为您使用的是 Tomcat 8080),否则下面的概念也会成立。一旦将应用程序部署到容器上(假设容器是 运行 on 8080),您将请求应用程序可能 URL http://IP:8080/appName/. In this case, there is no conflict of ports (actually conflict scenario does not apply). You have mentioned your angular works on port 3000, by which I assume you have hard coded the service URL as (probably) http://localhost:3000/. Now this would not work as the server is running on 8080. What you need to do is relativise the service invocation URLs in your application or hard code http://localhost:8080/ for your service URLs in Angular application. There is also another consideration, if you are using IP to access the application with browser and you are using hardcoded localhost in your service invocation URL, then the cross-domain problem would come. To avaid this use the URL as http://localhost:8080 ... 从浏览器。但是,如果您在 Angular 应用程序中相对化服务调用 URL,那么您可以使用浏览器中的 IP 或本地主机。