如何修复DOMException:无法在“ XMLHttpRequest”上执行“打开”:调用第三方服务器/ API时无效的URL Angular HttpClient?

时间:2019-05-07 16:18:01

标签: javascript angular xmlhttprequest

我收到以下错误:

DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL

在执行GET对debounce.com的请求时

import{HttpClient} from '@angular/common/http';

export {ThisClass} {

constructor(private http:HttpClient){
}

validateEmailBounce(email) {
    const url = 'https://api.debounce.io/v1/?api=my_api_key&email=' + email;
    console.log(url); // https://api.debounce.io/v1/?api=my_api_key&email=someone@email.com
    return this.http.get(url);
  }
}

1 个答案:

答案 0 :(得分:0)

您可能必须尝试对URL参数进行编码。

const url = 'https://api.debounce.io/v1/?api=my_api_key&email=' + encodeURIComponent(email);

文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent