cross domain posts to ASP.Net MVC app

混江龙づ霸主 提交于 2019-12-18 21:02:12

问题


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?


回答1:


I fiddled with my ajax call and it seems to be working (compare to the ajax call above):

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

I removed "contentType: 'application/json'" and "JSON.stringify(...)" calls and I'm able to post to the server.

I'm not sure how to explain why it's working. Any ideas? Are there any security issues? I'm doing this all on my laptop. I set up 2 different websites via IIS 7. Will this make a difference?




回答2:


Internally the JSONP response (the default type for cross-domain requests) is fetched by injecting a <script> tag, which points to the URL. Because of that, only GET method is possible with JSONP. Other methods will be ignored and fall back to GET.




回答3:


When you specified the crossDomain property to "true", the dataType property gets set as jsonp. You will need a way to handle this jsonp however on the MVC side. You might want to take a look at the following stackoverflow post: ASP.net MVC returning JSONP




回答4:


you have two options, in the dataType you can put text or jsonp instead of json. and if you give us and example of the data that your are sending this is going to be more easy .

Regards




回答5:


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.



来源:https://stackoverflow.com/questions/9010457/cross-domain-posts-to-asp-net-mvc-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!