IE8 jquery ajax head request not working? - javascript

typical IE problems everyone. this works in chrome, firefox, safari - but not in IE8. haven't tried in IE9 yet. anyone know what i'm missing? i tried it with and without the crossDomain param. thanks in advance.
$.ajax({
url:'http://www.example.com/photo.jpg',
type:'HEAD',
crossDomain:true,
error: function() {
// something bad
},
success: function() {
// something cool
}
});

From here http://api.jquery.com/jQuery.ajax/:
type
Default: 'GET'
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

Related

IE6 Ajax with JQuery

I'm trying to fecth data from a REST webservice into a HTML page. The problem is Internet Explorer 6 (which is my target station on XP SP3) that I'm struggling to make work.
Here's the code used :
$.ajax({
type: "GET",
contentType:"application/json; charset=utf-8",
dataType : 'json',
url: "https://jsonplaceholder.typicode.com/posts/1",
success: function(data) {
alert(data);
},
complete: function(xhr) {
alert(xhr.status+" "+xhr.responseText);
}
});
Tested on Firefox 52 ESR : both success and complete functions works.
On Chrome 49 : success works, complete is called but xhr.status is 0 and xhr.responseText is empty.
On IE6 success is not called at all and complete is called but xhr.status is 0 and xhr.responseText is undefined.
Tried what was already answered here on SOF like removing extra commas, adding dataType ... but still no success with IE6.
How can we do it once for all ?
Thanks
IE6 is ancient, it does not support CORS (not even with XDomainRequest).
There is no way to perform cross-origin RESTful HTTP requests with JavaScript in IE6.
If you want to perform a cross-origin request, then you will need to use some other (non-RESTful) approach such as JSONP.
As Quentin said, sending CORS request is not supported in IE 6/7, you could check this blog.
You could refer to the following code to use JSONP.
// Using JSONP
$.ajax({
url: "<request url>",
jsonp: "callback",
// Tell jQuery we're expecting JSONP
dataType: "jsonp",
data: {
q: "select title,abstract,url from search.news where query=\"cat\"",
format: "json"
},
// Work with the response
success: function( response ) {
console.log( response ); // server response
}
});
Besides, please check this article:
Internet Explorer 9 and earlier ignores Access-Control-Allow headers and by default prohibits cross-origin requests for Internet Zone. To enable cross-origin access go to Tools->Internet Options->Security tab, click on “Custom Level” button. Find the Miscellaneous -> Access data sources across domains setting and select “Enable” option.

IE and Edge returns same response on AJAX request using jQuery

jQuery version: 2.1.4
Server-side technology: ASP.NET MVC 5
Erratic behaviour in browsers: Internet Explorer 11 and below. Also in latest Microsoft Edge
I'm doing an AJAX request like this:
$(document).ready(function(){
$.ajax({
url: "/actions/addPost",
type: "POST",
data: { name: "Jesús", ... },
sync: true,
success: function(data){
$("#blog-posts").load("/actions/getPosts");
},
error: function(data){
// show error ..
}
});
});
The load("actions/getPosts") will always return the same response, when It should return all the posts. Funny thing is, when I reload the page, it will load all the posts, also, if I request /actions/getPosts directly it will return every post correctly. But it's as if the requests had been cached somehow by IE and Edge. In every other browser, Chrome, Firefox, Opera and Safari, it works nicely.
What's the cause of this issue? How to fix it?

Ajax call on IE 9 (success data - undefined)

When calling specific service with ajax call IE9 returns "undefined":
$.ajax({
url: URL_PATH,
type: "get",
success: function(data) {
console.log(data);
}
});
Inspecting same code in Firefox, Chrome, IE10+, it works. I even tried adding:
contentType: "application/json; charset=utf-8",
datatype: "json",
to ajax call properties, but no luck.
Strange thing is that when I call a local JSON file, everything is okay in IE9, but when returning from local server url (information from database) that error occurs. Taking a look at response body, I got well formatted json string.
UPDATE:
Also added the error catching block:
error: function(XMLHttpRequest)
{
console.log(XMLHttpRequest);
},
but no luck, it doesn't go inside the error block, it catches "success"
I found a problem. The response header of my rest service was set to charset=UTF8 and the IE couldn't recognize that while other browsers work with no problem, the correct spelling needs to be UTF-8 with a dash :) a rookie mistake. Thanks everyone for suggestions. Closing this thread now.
there are 3 ways to solve this issue,
First one add following lines,
<!--[if lte IE 9]>
<script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.0/jquery.xdomainrequest.min.js'></script>
<![endif]-->
Second one,
solve it by using dataType='jsonp' at the place of dataType='json'

Jquery .Get() pulls NULL in Internet Explorer

I have a problem with an AJAX call in JQuery. It works on Chrome, FF, and Safari, but not IE. In fact in IE nothing happens at all, no errors, no data loaded.
Here is the code:
$(document).ready(function() {
$.ajaxSetup({ cache: false });
$.get("ShoppingCart2.aspx", { }, function(data) {
//query the jq object for the values
alert(data);
alert($(data).find('#Items').text());
var intI = parseInt(($(data).find('#Items').html()));
With the alert data I find all the data from the page I am making a call from, but unfortunately my data.find methods pull up null for IE. I'm not sure if it's the code or the browser, but I am really stuck. Thank you for the help.
Edit: I did add in the cache: false command, but still I have no luck. I really cannot understand why this won't work in IE.
Try this (once in your page/external js, before your AJAX calls):
$.ajaxSetup({ cache: false });
IE likes to cache the hell out of things, and if you were testing and had no content there at one point, chances are IE is holding onto it. Using $.ajaxSetup() and and telling it by default to not cache AJAX results should resolve this. If you're curious, it's sticking a timestamp on the URL as a cache breaker under the covers, use fiddler to see this happening.
Is it perhaps caching the AJAX? What happens if you put this before your code:
$.ajaxSetup({ cache:false });
A quick solution without coding it could be to press CTR+F5 to clear the cache upon refresh.
Well I was unable to make the .find part of the .get work in internet explorer, but I did find a way to get the ajax information I needed:
var information = $.ajax({ type: "GET", dataType: "html", url: "ShoppingCart2.aspx", data: querystring, async: false }).responseText + " ";
This passes a query string to the website then gets information from the website back into one big string. I then
manipulated that string to get what I needed. Unfortunately it is a lot slower than the .get command, but it is a fix.
Thanks for the help everyone!

Ajax requests from xul not working in firefox 2

I'm working on a firefox extension and have been developing it in Firefox 3, I went to test it on Firefox 2 and for some reason, none of my HTTP requests is firing. The format of the requests are below (using prototype):
theResponse = function(response){
//some code
}
new Ajax.Request(url,{
method:'get',
parameters : {url: currentURL},
onSuccess: theResponse,
onFailure: function(){ alert('Something went wrong...') }
});
I have been trying to find a solution but the closest thing I've found is something to do with cross-site HTTPrequests, anyone has any ideas?
Figured out the problem was to do with the way prototype performs HTTPrequests, switched to using jquery and no further problems ... well with HTTPrequests anyway.

Categories