跨域职位,以ASP.Net MVC应用程序 [英] cross domain posts to ASP.Net MVC app

查看:211
本文介绍了跨域职位,以ASP.Net MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展的地方HTML和JavaScript块传送到不同的客户端应用程序。我能够通过增加获取HTML / JavaScript的块以下,以网络配置文件:

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>

这是做GETS伟大​​的工作。我快到的问题是这样做的POST使用jQuery跨域:

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.');
        }
    });

我有许多客户端占用我的应用程序(希望)。我想过使用代理,但我没有在客户端服务器的控制权,所以我不能够安装的HttpHandler充当代理服务器。

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.

我如何可以从不同的客户端POST JSON数据的任何建议跨域到我的ASP.Net MVC应用程序?

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

推荐答案

我拨弄着我的Ajax调用,它似乎是工作(比较上面的Ajax调用):

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.');
        }
    });

我删除的contentType:应用/ JSON'和JSON.stringify(...)电话和我能够发布到服务器

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

我不知道该怎么解释为什么它的工作。有任何想法吗?是否有任何安全问题?我做的这一切都在我的笔记本电脑。我通过IIS 7,建立2个不同的网站,这是否会有所作为?

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?

这篇关于跨域职位,以ASP.Net MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆