以下适用于IE以外的所有浏览器(我在IE 9中测试).
jQuery.support.cors = true;
...
$.ajax(
url + "messages/postMessageReadByPersonEmail",
{
crossDomain: true,
data: {
messageId : messageId,
personEmail : personEmail
},
success: function() {
alert('marked as read');
},
error: function(a,b,c) {
alert('failed');
},
type: 'post'
}
);
Run Code Online (Sandbox Code Playgroud)
我有另一个使用的函数dataType: 'jsonp',但我不需要在这个AJAX调用上返回任何数据.我的最后一招将是返回JSONP中包含的一些乱码,以使其正常工作.
任何想法为什么IE搞砸了没有返回数据的CORS请求?
这个问题从IE9开始,对于POST请求,contentType必须是text/plain,并且application/json将不起作用.
我添加了moonscript并继续使用contentType: text/plain.我还将自定义媒体类型添加到api中,如下面的多种表单所示:
并添加了text/plain媒体类型的插入WebApiConfig
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
// allows 'text/plain' as a supported media type
config.Formatters.Add(new TextMediaTypeFormatter());
Run Code Online (Sandbox Code Playgroud)
但是,当在IE9中发布(使用仿真)时,我仍然收到了 415 Unsupported Media Type
Key Value
Response HTTP/1.1 415 Unsupported Media Type
$.ajax({
type: "POST",
url: hope_forms.viivApiUrl + 'newsletter',
contentType: 'text/plain',
data: JSON.stringify(model),
success: function (data) {
.....
},
error: function (responseText) {
console.log(responseText)
modal.showModal('Something went wrong, please …Run Code Online (Sandbox Code Playgroud) c# jquery internet-explorer-9 asp.net-web-api mediatypeformatter
我无法弄清楚为什么下面的代码在FF中完美运行但在IE9中却没有.
脚本的作用是,点击一个按钮,它从一个表单中选择一些像街道,邮政编码和城镇的值,构建一个字符串,将其提交给谷歌,并返回该地址的纬度和长度(并将其放入两个表格中的字段)
$("#google_coordinates").live("click", function(){
var string = encodeURIComponent($('#sAddressStreet1').val())+",";
if($('#sAddressStreet2').val() != ""){
string += encodeURIComponent($('#sAddressStreet2').val())+",";
}
string += encodeURIComponent($('#sAddressPostalcode').val())+",";
string += encodeURIComponent($('#sAddressTown').val())+",";
string += encodeURIComponent($('#sAddressCountryCode option:selected').text());
/* test with an alert - string shows fine in both FF and IE */
alert(string);
$.get("http://maps.googleapis.com/maps/api/geocode/xml?address="+string+"&sensor=false", function(xml){
$(xml).find('location').each(function() {
lat = $(this).find('lat').text();
lng = $(this).find('lng').text();
/* test with an alert - fine in FF not in IE */
alert(lat + ',' + lng);
$("#sAddressLatitude").val(lat);
$("#sAddressLongitude").val(lng);
});
});
return false;
});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,此脚本提交的网址是:= 1363342459585"> http://maps.googleapis.com/maps/api/geocode/xml?address=Servicev%C3%A4gen%207,311%2033,Falkenberg,Sweden&sensor=false& = …
我试图用ajax调用来测量下载速度.这是我的代码
var start = new Date();
$.ajax ({
url: 'https://www.example.com/perftest/dummyFile1024',
cache: false,
success : function() {
var total = (new Date() - start)
alert(total)
},
error : function(jqxhr, status, ex) {}
})
Run Code Online (Sandbox Code Playgroud)
它不会等到整个文件加载.当我添加async: false,它等待加载整个文件,我能够测量chrome和safari的带宽但是Internet Explorer和Firefox仍然工作相同,因为async: true,他们不等到整个文件加载.您是否知道如何管理它适用于IE和Firefox?谢谢.
如何正确使用XDomainRequest重写Ajax请求以使其在IE 8 +中工作?
$.ajax({
type: "GET",
url: url,
success: function(xml) {
$('.post-msg').append(processXml(xml, config));
},
error: function(jqXhr, textStatus, errorThrown) {
var errorMsg = "Request on url: " + url + " failed: " + textStatus + " error:" + errorThrown;
alert(errorMsg);
}
});
Run Code Online (Sandbox Code Playgroud) jquery ×5
ajax ×3
javascript ×2
asynchronous ×1
c# ×1
cors ×1
cross-domain ×1
get ×1
google-api ×1
jsonp ×1