角形式的问题后CORS问题可以通过代理解决

时间:2019-03-22 09:35:59

标签: angular typescript cors

我正在使用Angular reactive forms,我想将表单发布到本地主机的url中。让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(内部   服务器错误)

0 个答案:

没有答案