4
votes

I want to use the Azure Service Bus REST API in JavaScript. Unfortunately I'm running into cross origin problems, as my Service Bus namespace is not in the same domain as my site.

I know that you can enable CORS with many Azure services, but I can't find any resource in the Azure documentation or elsewhere telling me how to do this for the Service Bus.

Does anybody know if, and how, this can be done?

3

3 Answers

1
votes

I've solved this issue by creating a separate Web App with the following Web.config which is just a simple proxy.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Proxy" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{CACHE_URL}" pattern=".*" />
          </conditions>
          <action type="Rewrite" url="https://mynamespace.servicebus.windows.net/{R:0}" logRewrittenUrl="true" />
      <serverVariables>
        <set name="HTTP_ACCEPT_ENCODING" value="" />
      </serverVariables>
    </rule>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Expose-Headers" value="BrokerProperties" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>
1
votes

Azure Service Bus REST API supports CORS by default. All origins are accepted.

-3
votes

To be honest this is not an Azure specific. If you are using jQuery you need to enable cross-domain request option, read more here http://api.jquery.com/jQuery.ajax/. There is also a JavaScript ServiceBus SDK: https://github.com/ddobric/ServiceBusJavaScriptSdk

Generally from the purity point of view calling Service Bus from client side doesn't look good, I'd rather wrap that call into your own API. But I don't know your scenario and it might be valid.