Я использую Angular reactive forms
, и я хочу опубликовать форму в URL от моего localhost.Пусть URL-адрес будет example.com
, когда я отправлю сообщение, он вернет OPTIONS method
с ошибкой, и я использовал прокси для решения проблемы CORS
, но это не работает. Я поделюсь тем, что сделал
Используется для публикации:
onSubmit(){
this.http.post(this.nodeuploadurl,
this.Form1.value).subscribe((data) =>
{console.log("success",data)},
(error)=>{console.log("error",error)});
}
proxy.config.json:
{
"/angular": {
"target": {
"host": "example.com",
"protocol": "https:",
"port": 443
},
"changeOrigin": true,
"logLevel": "info"
}
}
package.json:
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.conf.json", <---
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
}
В cmd:
10% building 3/3 modules 0 active*[HPM] Proxy created: /angular -> { host: 'example.com', protocol: 'https:', poDate: 2019-03-22T06:44:02.929Z*
Hash: 531b6e7427472a61a852
Time: 25966ms
chunk {main} main.js, main.js.map (main) 308 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 245 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.08 kB [entry] [rendered]
chunk {scripts} scripts.js, scripts.js.map (scripts) 464 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 2.99 MB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 9.75 MB [initial] [rendered]
В консоли при отправке формы:
OPTIONS http://example.com 404 (Not found: Could not find the controller.)
4234:1 Access to XMLHttpRequest at 'http://example.com' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
ОБНОВЛЕНО:
После изменения this.http.post(this.nodeuploadurl,this.Form1.value)
на this.http.post(angular/something,this.Form1.value)
происходит этот 500 (внутренняя ошибка сервера). Как преодолеть это?
POST http://localhost:4200/angular/something 500 (Внутренняя ошибка сервера)