跨域发布到 ASP.Net MVC 应用程序
我正在开发一个应用程序,其中 HTML 和 javascript 块被传递到不同的客户端。我可以通过将以下内容添加到 Web 配置文件来获取 html/javascript 块:
<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 非常有用。我遇到的问题是使用 jQuery 进行跨域 POST:
$.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 来充当代理。
关于如何将来自不同客户端的 json 数据跨域发布到我的 ASP.Net MVC 应用程序,有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我摆弄了我的 ajax 调用,它似乎正在工作(与上面的 ajax 调用相比):
我删除了“contentType: 'application/json'”和“JSON.stringify(...)”调用,我能够发布到服务器。
我不知道如何解释它为什么有效。有什么想法吗?是否存在安全问题?我正在我的笔记本电脑上完成这一切。我通过 IIS 7 设置了 2 个不同的网站。这会有所不同吗?
I fiddled with my ajax call and it seems to be working (compare to the ajax call above):
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?
在内部,JSONP 响应(跨域请求的默认类型)是通过注入指向 URL 的
标签来获取的。因此,JSONP 只能使用
GET
方法。其他方法将被忽略并回退到GET
。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, onlyGET
method is possible with JSONP. Other methods will be ignored and fall back toGET
.当您将 crossDomain 属性指定为“true”时,dataType 属性将设置为 jsonp。然而,您将需要一种在 MVC 端处理此 jsonp 的方法。您可能需要查看以下 stackoverflow 帖子:ASP.net MVC 返回 JSONP
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
你有两个选择,在 dataType 中你可以输入 text 或 jsonp 而不是 json。如果您向我们提供您要发送的数据示例,这将变得更加容易。
问候
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
如果您可以控制所包含的 JS 库,那么最好的方法是使用众多跨域通信库之一。客户端(在您的情况下是 MVC 应用程序)必须将其配置为接受此类请求,在大多数情况下,这意味着在接受端具有相同的库。
到目前为止我发现的最好的一个是 EasyXDM。没有太多限制,并且可以根据浏览器功能进行调整。但你必须在两个相互通信的应用程序上都安装它。
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.