gpt4 book ai didi

jquery - Cors、Web Api、IE8、发布复杂数据

转载 作者:行者123 更新时间:2023-12-01 01:15:25 25 4
gpt4 key购买 nike

作为我工作环境的一部分,我们需要支持 IE8,但希望推进技术,特别是 CORS。

我在将复杂对象发布到 ie8 中的 cors 服务时遇到问题。该对象为空。以下是重现的步骤。如果需要,我可以将项目上传到 github。

我创建了一个新的 mvc4 项目。添加了 API Controller 。并进行了以下更改。

支持预检复杂的 cors 调用 (global.asax):

    protected void Application_BeginRequest()
{
//This is needed for the preflight message
//https://stackoverflow.com/questions/13624386/handling-cors-preflight-requests-to-asp-net-mvc-actions
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS") { Response.Flush(); }
}

来源:Handling CORS Preflight requests to ASP.NET MVC actions

支持text/plain(ie8仅发送带有cors的text/plain)(global.asax):

    protected void Application_Start()
{
//This is needed to support text/plain
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
config.Formatters.Remove(config.Formatters.XmlFormatter);

...
}

信用:Posting text/plain as a complex object in WebAPI with CORS

支持动词以外的其他函数名称(put/post/etc)(WebApiConfig.cs)"

    public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "APICustom",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

...
}

支持cors(web.config)

<httpProtocol>
<customHeaders>
<!-- cors -->
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>

API Controller ,我称之为PersonController.cs

 public class PersonController : ApiController
{

public List<string> Get()
{
List<string> s = new List<string>();
s.Add("s");
s.Add("t");
s.Add("u");
return s;
}



[Serializable()]
public class BaseReply
{
public bool successful = true;
public string error;
}
[Serializable()]
public class UpdateSomethingReply: BaseReply
{
public UpdateSomethingRequest request;
public List<string> stuff = new List<string>();
}
[Serializable()]
public class UpdateSomethingRequest
{
public int hasInt;
public string hasString;
}
//[FromBody]
[HttpPost]
public UpdateSomethingReply UpdateSomething([FromBody] UpdateSomethingRequest request)
{
string body = Request.Content.ReadAsStringAsync().Result;
UpdateSomethingReply reply = new UpdateSomethingReply();
reply.request = request;

reply.stuff.Add("v");
reply.stuff.Add("w");
reply.stuff.Add("x");
return reply;
}

这就是服务变化的程度。接下来我创建一个客户端。这也是一个mvc4项目。这里有一些非常基本的东西。

使用 cors 填充 ie8 (index.cshtml):

<script src="~/Scripts/jQuery.XDomainRequest.js"></script>

来源:https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

调用cors服务

 $(document).ready(function () {
$.when(
$.ajax({
url: urls.person.UpdateSomething,
type: 'post',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
hasInt: 1,
hasString: "u"
})
})
)
.fail(function (jqXHR, textStatus, errorThrown) {
})
.done(function (data) {
console.log(JSON.stringify(data));
});

$.when(
$.ajax({
url: urls.person.Get,
dataType: 'json'
})
)
.fail(function (jqXHR, textStatus, errorThrown) {
})
.done(function (data) {
console.log(JSON.stringify(data));
});

$.when(
$.ajax({
url: urls.person.UpdateSomething,
type: 'post',
contentType: "text/plain",
dataType: 'json',
data: JSON.stringify({
hasInt: 1,
hasString: "u"
})
})
)
.fail(function (jqXHR, textStatus, errorThrown) {
})
.done(function (data) {
console.log(JSON.stringify(data));
});
});

正如我之前所说,所有 3 个调用都在 ie8 中完成。但是服务中的请求对象在 ie8 中为空,在 Firefox 中它被填充,即使我强制内容类型为文本/纯文本

IE8 控制台输出:

{"request":null,"stuff":["v","w","x"],"successful":true,"error":null}

Firefox 控制台输出:

{"request":{"hasInt":1,"hasString":"u"},"stuff":["v","w","x"],"successful":true,"error":null}

2013 年 9 月 25 日更新

我可以确认正文正在发送,但没有被 Web api 解析。如果我添加以下 hack 它将按预期返回数据。在 Firefox 中,正文将为空,并填充请求对象。在 ie8 中,主体仍然包含内容,并且请求为空。

    [HttpPost]
public UpdateSomethingReply UpdateSomething(UpdateSomethingRequest request)
{
if (request == null && Request.Content.ReadAsStringAsync().Result !="")
{
request = JsonConvert.DeserializeObject<UpdateSomethingRequest>(Request.Content.ReadAsStringAsync().Result);
}

UpdateSomethingReply reply = new UpdateSomethingReply();
reply.request = request;
reply.body=Request.Content.ReadAsStringAsync().Result;
reply.headers = Request.Headers.ToString();
reply.stuff.Add("v");
reply.stuff.Add("w");
reply.stuff.Add("x");
return reply;
}

最佳答案

这是我正在谈论的代码。将其创建为一个新类,我在 WebAPI 项目中创建了一个 DelegatingHandlers 文件夹(但话又说回来,我还有一个过滤器文件夹、一个模型绑定(bind)文件夹...)

我添加了大量评论,您可以轻松删除它们。

下面假设 IE 8/9 将始终发送“JSON”数据。如果您的 webAPI 实现允许内容协商,并且您希望在 IE8/9 中包含该功能,那么您显然需要在下面的代码中添加一些 if 语句,但这应该足以让您继续下去。我个人只是声明我只接受 IE 8/9 的 JSON。

namespace REDACTED.WebApi.DelegatingHandlers
{
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// Gives the WebAPI the ability to handle XDomainRequest objects with embedded JSON data.
/// </summary>
public class XDomainRequestDelegatingHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
// XDomainRequest objects set the Content Type to null, which is an unchangable setting.
// Microsoft specification states that XDomainRequest always has a contenttype of text/plain, but the documentation is wrong.
// Obviously, this breaks just about every specification, so it turns out the ONLY extensibility
// point to handle this is before the request hits the WebAPI framework, as we do here.

// To read an apology from the developer that created the XDomainRequest object, see here:
// http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

// By international specification, a null content type is supposed to result in application/octect-stream (spelling mistake?),
// But since this is such an edge case, the WebAPI framework doesn't convert that for us before we hit this point. It is unlikely,
// but possible that in a future Web.Api release, we will need to also sniff here for the octect header.
if (request.Content.Headers.ContentType == null)
{
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}

return base.SendAsync(request, cancellationToken);
}
}
}

我的 WebAPIConfig 文件如下所示:

        public static void Register(HttpConfiguration config)
{
// Normal config.Routes statements go here

// Deserialize / Model Bind IE 8 and 9 Ajax Requests
config.MessageHandlers.Add(new XDomainRequestDelegatingHandler());
}

然后为了确保我的 POST 调用符合 IE 8 和 9 兼容,我在 JS 中添加了以下内容(当然,如果您也使用自己的 API,则显然只需要包含此内容)

esbPost: function (apiUrl, apiData, fOnSuccess, fOnFailure) {
$.support.cors = true; // Not sure that I need this.

var testModernAjax = function () {
if (window.XMLHttpRequest) {
var testRequest = new XMLHttpRequest;

// IE 8 / 9 with jQuery can create XMLHttpRequest objects, but only modern
// CORS implementing browsers (everything + IE10) include the withCredentials specification.
if ('withCredentials' in testRequest) {
return true;
}
return false;
}
return false;
};

var testMsieAjax = function () {
if (window.XDomainRequest) {
return true;
}
return false;
};

//All browsers, and IE 10
if (testModernAjax()) {
$.ajax({
url: apiUrl,
type: 'POST',
dataType: 'json',
data: apiData,
success: function (result) {
if (fOnSuccess) {
fOnSuccess(result);
}
},
error: function (jqXHR, textStatus, errorThrown) {
if (fOnFailure) {
fOnFailure(jqXHR, textStatus, errorThrown);
}
}
});
//IE 8 / 9
} else if (testMsieAjax()) {
var xdr = new XDomainRequest();
xdr.onload = function () {
var parsedResponse = $.parseJSON(xdr.responseText);
if (fOnSuccess) {
fOnSuccess(parsedResponse);
}
};
xdr.onerror = function () {
if (fOnFailure) {
fOnFailure();
}
};
xdr.onprogress = function () { };
xdr.open("post", apiUrl);
xdr.send(JSON.stringify(apiData));
} else {
// IE 7 can only do AJAX calls through a flash/iframe exploit, earlier do not include ajax support.
throw new 'This browser is unsupported for this solution.';
}
},

就我个人而言,我使用 JSONP 进行 GET,而不使用 PUTS 或 DELETES,所以这对我来说就足够了。如果我重新做这个项目,我会使用 PUTS 和 DELETES。为了使 IE 8/9 处理跨域 PUTS 和 DELETES,其显然常见的做法是在正在发送的数据上或 header 中包含一个新节点,称为“Type”的某种变体,并使用字符串“PUT”或“DELETE” ”。但我不确定我会在哪里嗅出它。

启用 CORS 就像将以下内容放入 Web.Config 中一样简单。

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<!--<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />-->
</customHeaders>
</httpProtocol>

正如您在上面的评论中看到的,您还可以通过原始 url(*)和请求类型(put、post 等)来限制 CORS。完全使stuff like this 完全没有必要。 This guy's blog gives a really good walkthrough.

这实际上就是您需要对全新的 WebAPI 项目执行的所有操作,使其支持 CORS 和 IE 8/9。

关于jquery - Cors、Web Api、IE8、发布复杂数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com