首页 > 解决方案 > 在获取数据时将 XMLHttpRequest 转换为 http 请求

问题描述

我正在尝试使用 http get 方法根据从后端提交的名称搜索数据..我收到以下错误..

从源“http://localhost:4200”访问“localhost:3300/secure/students/search/?uname=def”的 XMLHttpRequest 已被 CORS 策略阻止:跨源请求仅支持协议方案:http,数据,铬,铬扩展,铬不受信任,https。

这是我来自 component.ts 文件的代码:-(我从模板文件中的表单中获取数据)

    export class SearchStudentComponent implements OnInit {

  `uname :string = ' ';
  constructor(private http: HttpClient) { }
  
  `ngOnInit(): void {`
        
    }`
    

    Searchform(searchform:NgForm)
    {
         this.Uname = searchform.value.uname;
         console.log("user name is"+  this.Uname);
         console.log('localhost:3300/secure/students/search/?uname='+ this.Uname);
         this.http.get('localhost:3300/secure/students/search/?uname='+ this.Uname).subscribe(data=>console.log(data));
    }

标签: typescripthttpserverget

解决方案


推荐阅读