Alex Rivera | Logout

cross domain posts to ASP.Net MVC app

Asked 2012-01-25T21:19:51.233
10

I'm developing an app where HTML and javascript chunks are delivered down to different clients. I'm able to GET the html/javascript chunks by adding the following to web config file:

  <system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
  <httpProtocol>
      <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Headers" value="Content-Type" />
          <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
      </customHeaders>
  </httpProtocol>

This is working great for doing GETS. The problem I'm running into is doing POSTs cross domain using jQuery:

        $.ajax(
    {
        type: 'POST',
        url: url,
        crossDomain: true,
        data: JSON.stringify(data),
        dataType: 'json',
        contentType: 'application/json',
        success: function(responseData, textStatus, jqXHR) 
        {
            alert('Success');
        },
        error: function (responseData, textStatus, errorThrown) 
        {
            alert('POST failed.');
        }
    });

I will have numerous clients consuming my app (hopefully). I thought about using a proxy, but I do not have control of the client servers so I'm not able to install a httpHandler to act as a proxy.

Any suggestions on how I can POST json data from different clients cross domain to my ASP.Net MVC app?

Edit
Report

1 Answer

0

If you have control over what JS libraries you include, the best way is to use one of many cross-domain communication libs. The client (MVC app in your case) will have to have it configured to accept such requests, in most cases it means to have the same lib on accepting side.

The best one I found so far is EasyXDM. Not many limitations and adjusts itself to browser capabilities. But you have to have it on both apps that talk to each other.

answered 2012-03-22T14:13:44.987

Your Answer