作者: 老葛 亚艾元软件
到app.module.ts文件中注册一下http module,示例代码如下:
import { HttpModule} from '@angular/http';
@NgModule({
imports: [
HttpModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {
}
添加到imports数组里面。
使用http发起请求,出现如下错误信息:
XMLHttpRequest cannot load http://localhost/example/caselistjson. Redirect from 'http://localhost/example/caselistjson' to 'http://localhost/example/user/login?destination=caselistjson' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
这个问题就是跨越问题,解决办法参考如下:
在网站的.htaccess文件中,添加以下代码:
Header set Access-Control-Allow-Origin "*"
之后请求成功的,按时返回数据错误。检查发现用户没有权限。
降低views的权限设置,允许外部访问。