类型错误,由于尝试解决跨域错误而引发。

huangapple go评论81阅读模式
英文:

TypeError from trying to resolve CORS error

问题

I have [Vue warn] TypeError from trying to resolve CORS error.

Here is my code saving data to server.

saveToServer() {
  // Upload Code
  let formData: any = new FormData()
  formData.append(
    '0c42819f7026a5b7f407c011c0b9523dad249b9ceef29cddcc5140b6ab922867',
    ''
  )
  formData.append(
    'robot_file',
    new Blob([JSON.stringify(this.robot)]),
    `${this.taskName}.json`
  )
  formData.append(
    'robot_json',
    new Blob([this.robot.toRobot(0)]),
    `${this.taskName}.robot`
  )

  axios
    .post(
      'http://ideasolutions.co.kr/bpm/common/insertRpaProcessByDeviceList',
      formData,
      { headers: formData.getHeaders() }
    )
    .then((data: any) => {
      console.log('Upload End')
      console.log(data)
    })
}

To resolve CORS error, I added

axios.post('', formData, { headers: formData.getHeaders() }, ...)

but it didn't work.

After adding that code, CORS warning was gone, but TypeError came.

[Vue warn]: Error in v-on handler: "TypeError: formData.getHeaders is not a function"

found in

---> <VBtn>
       <VAppBar>
         <ModelCanvas> at src/components/designer/ModelCanvas.vue
           <VApp>
             <App> at src/App.vue
               <Root>
TypeError: formData.getHeaders is not a function
    at VueComponent.ModelCanvas.saveToServer (ModelCanvas.vue?ba42:162)
    at click (ModelCanvas.vue?4f3d:93)
    at invokeWithErrorHandling (vue.runtime.esm.js?c320:3017)
    at VueComponent.invoker (vue.runtime.esm.js?c320:1815)
    at invokeWithErrorHandling (vue.runtime.esm.js?c320:3017)
    at VueComponent.Vue.$emit (vue.runtime.esm.js?c320:3716)
    at VueComponent.click (vuetify.js?ed19:2564)
    at invokeWithErrorHandling (vue.runtime.esm.js?c320:3017)
    at HTMLButtonElement.invoker (vue.runtime.esm.js?c320:1815)
    at HTMLButtonElement.original_1._wrapper (vue.runtime.esm.js?c320:7473)
英文:

I have [Vue warn] TypeError from trying to resolve CORS error.

Here is my code saving data to server.

saveToServer() {
  // Upload Code
  let formData: any = new FormData()
  formData.append(
    &#39;0c42819f7026a5b7f407c011c0b9523dad249b9ceef29cddcc5140b6ab922867&#39;,
    &#39;&#39;
  )
  formData.append(
    &#39;robot_file&#39;,
    new Blob([JSON.stringify(this.robot)]),
    `${this.taskName}.json`
  )
  formData.append(
    &#39;robot_json&#39;,
    new Blob([this.robot.toRobot(0)]),
    `${this.taskName}.robot`
  )

  axios
    .post(
      &#39;http://ideasolutions.co.kr/bpm/common/insertRpaProcessByDeviceList&#39;,
      formData,
      { headers: formData.getHeaders() }
    )
    .then((data: any) =&gt; {
      console.log(&#39;Upload End&#39;)
      console.log(data)
    })
}

To resolve CORS error, I added

axios.post(&#39;&#39;, formData, {headers: formData.getHeaders()}, ...)

but it didn't work.

After adding that code, CORS warning was gone, but TypeError came.

[Vue warn]: Error in v-on handler: &quot;TypeError: formData.getHeaders is not a function&quot;

found in

---&gt; &lt;VBtn&gt;
       &lt;VAppBar&gt;
         &lt;ModelCanvas&gt; at src/components/designer/ModelCanvas.vue
           &lt;VApp&gt;
             &lt;App&gt; at src/App.vue
               &lt;Root&gt;
TypeError: formData.getHeaders is not a function
    at VueComponent.ModelCanvas.saveToServer (ModelCanvas.vue?ba42:162)
    at click (ModelCanvas.vue?4f3d:93)
    at invokeWithErrorHandling (vue.runtime.esm.js?c320:3017)
    at VueComponent.invoker (vue.runtime.esm.js?c320:1815)
    at invokeWithErrorHandling (vue.runtime.esm.js?c320:3017)
    at VueComponent.Vue.$emit (vue.runtime.esm.js?c320:3716)
    at VueComponent.click (vuetify.js?ed19:2564)
    at invokeWithErrorHandling (vue.runtime.esm.js?c320:3017)
    at HTMLButtonElement.invoker (vue.runtime.esm.js?c320:1815)
    at HTMLButtonElement.original_1._wrapper (vue.runtime.esm.js?c320:7473)

答案1

得分: 1

首先,FormData 没有 getHeaders 函数。

要解决跨域错误,不能在前端(客户端)处理,必须从服务器端处理,通过在服务器上启用客户端的来源。

如果你的服务器基于 express.js,请参考这里

英文:

Firstly, FormData don't have function getHeaders.

To resolve CORS error, you can't do it from front-end (client-side), you must handle it from server-side by enable client's origin on server.

If your server base on express.js, please reference here

huangapple
  • 本文由 发表于 2023年7月11日 09:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定