-1

An Aurelia SPA uses fetch against a different origin server. A preflight OPTIONS request is sent and because the server is configured to respond, the response is 200 OK.

But that's all that happens.

What must I add to make the rest of it happen?

Here's the request.

  this.http.fetch(this.LabelServiceUrl, {
    method: "post",
    mode: "cors",
    headers: {
      "Content-type": "application/json; charset=UTF-8"
    },
    body: json({
      printer: this.LabelPrinter,
      html: html
    })
  }).then(json)
    .then(response => {
      console.log('Success:', JSON.stringify(response))
    })
    .catch(error => {
      console.error('Error:', error)
    });

IIS is configured by web.config

<system.webServer>
 <httpProtocol>
   <customHeaders>
     <add name="Access-Control-Allow-Origin" value="*" />
   </customHeaders>
 </httpProtocol>
</system.webServer>
Peter Wone
  • 17,965
  • 12
  • 82
  • 134
  • Your system.webServer needs to also set an Access-Control-Allow-Headers response header with the value 'Content-type'. That’s because your request is setting a Content-type header. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers. And your config also needs to set an Access-Control-Allow-Methods response header with the value 'POST. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods'. And see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests – sideshowbarker Oct 04 '18 at 22:00

1 Answers1

0

You don't have to do anything special, magic happens and the request is then sent with a normal response.

If this fails to happen, as it did in my case, this is because something in your code is not kosher but the error is getting lost somewhere in the handshaking and it all just stops.

Put a test page in your web service so you can exercise the web api without CORS. When the error is resolved test again with your app. In my case the problem was the perennial headache of receiving complex parameters from a post into an object graph; my classes weren't quite right for WebAPI magic parsing.

Peter Wone
  • 17,965
  • 12
  • 82
  • 134