Jquery .Get() pulls NULL in Internet Explorer - javascript

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!

Related

Syncronous XMLHTTPRequest deprecated crash browser?

I have to read information as below from a microprocessor hardware
var save_it = '';
$.ajax({
type: "GET",
url: 'http://www.micro-processor-server/?ROBOT=arm_controle',
async: false,
success:function(m) {
save_it = m;
}
}).responseText;
console.log(save_it);
While doing so, it works, but my browser console gives very scary warning as below, also several times I have noticed web-browser Google chrome get hang:
Can anyone please show an alternative way how to make my Ajax query compatible? (I can't change codes in Micro-processor its third party robot)
Put the code that needs the response in the success function. You could also use $.get if this is all you're needing to do.
$.get('http://www.micro-processor-server/?ROBOT=arm_controle', function(data) {
console.log(data);
});
call another function with return data.
$.get('http://www.micro-processor-server/? ROBOT=arm_controle',function(data){
myfun(data);
});
myfun(data){
console.log(data);//here you might be able get rid of asynchronous behaviour.
}

Undefined index on ajax call only for one function

I have several different ajax calls on the same php page, but I get undefined index for only one function (createAlbum).
I send exactly the same parameters in each of my ajax calls. It worked fine for a few tests, but now it only works for the other ones, but not for this specific call.
I suspected that the .js file was still in the browser cache, so I cleared it and tried with other browsers, which worked for a few more attempts.
Now, I can't get it working on any browser, and definitely don't understand why.
Here is my ajax call :
$.ajax({
type: "POST",
url: BASE_URL,
data: {
action: "createAlbum",
data: JSONAlbum
},
cache: false,
success: callback
});
My php file handling the request ($_POST['action'] is always undefined with the above request) :
if (isset($_POST['action'])) {
switch ($_POST['action']) {
// Handle the action
// ...
}
} else {
echo 'ACTION : '.$_POST['action'];
}
The header containing the ajax parameters ("data" is a json containing one or more blob images, this might be the problem, but I didn't find anything saying so) :
And finally, the response containing the error :
I really hope this is not a dumb error of mine, but I asked a friend before posting, and he can't find the solution either.
Thanks for your help !
You said that the error not happens all the time (in some call yes, in other no).
The body post is sent, you see it in console. So we can exclude javascript problem.
So the problem is something that happens before or during the process.
PHP has some limitations on input vars, you can see it in php.ini.
I see that you send images in base64, so it's probable that something of these limitations are triggered.
See for example:
max_input_time
max_input_vars
post_max_size

Ajax call keeps getting blocked by browser

I am a bit stuck with an issue.
I am developing a small mobile website. I am trying to call a webservice using an ajax call, but the browser keeps blocking my call. If I start up Chrome using the tags... "--allow-file-access-from-files --disable-web-security​​" Then the call works perfectly. I have no issues whatsoever.
Now my problem is if I host the website, the browser is going to block my ajax call and the user cannot for example login or retrieve information. I present my ajax call below...
$.ajax({
async: true,
beforeSend: function () {
},
complete: function () { },
type: 'POST',
url: 'https://MySecretUrl.com/login?format=json',
contentType: 'application/json',
dataType: 'json',
data: '{"UserId":"mySecretUserId","Password":"mysecretPassowrd"}',
success: function (resultMessage) {
if (resultMessage.WasSuccessful == true) {
alert('YAY');
} else {
alert('Semi Yay');
}
},
error: alert('OOOOPS')
});
Does anybody know a workaround for getting information from the webservice without any browser blocking the ajax call ?
Any suggestions would be greatly appreciated.
Thanks in advance for the help.
EDIT
Hi Guys, Ok so I did some more digging and discovered the following.
When the request is made with browser security, the call changes the POST to a OPTIONS. this is called a preflighted request. One workaround that I have found is if you are making a GET call, then you can use jsonp as your data type. But now my problem is that it is incompatible with POST. Is there any fix that does not require the webservice to be changed ?
Is there any fix that does not require the webservice to be changed ?
No. If changing the webservice isn't an option, your only option is to not use the browser to make this request.
You must either make the server return the data in a format that can be accepted cross-domain, or don't make cross-domain requests with the browser.

javascript/Ajax only working when debugging

I wanted to remove the user session and related data when the user closes the browser. i referred to this (http://weblogs.asp.net/kaushal/archive/2011/02/25/user-is-trying-to-leave-trap-and-set-confirm-alert-on-browser-tab-close-event.aspx) post and i have some coding as below. I wanted to delete the user data from the application state and SQL server database when the user closes the browser. And i am doing this with the help of a handler.
The below code is working fine while debugging, it is calling the handler and executing all the codes which i have written in to it. But when i really use this app this is not working. I don't know where the problem is. And it is eating my time.
(I am using this inside the content of a Master page)
window.onbeforeunload = confirmExit;
function confirmExit() {
var UserID = '<%= Session["UserID"] %>';
$.ajax({
type: "POST",
url: "../../Handlers/HandlerUser.ashx?Action=RemoveUserDataOnBrowserClose&UserID=" + UserID.toString(),
contentType: "application/json; charset=utf-8",
async: false,
success: function (data) {
}
});
}
if any one can suggest another way to achieve the same, it will be a great help.
My guess is it is not working because it is asynchronous call.
the function exits before the call happens.
I suggest you interrupt the closing action, clean the data from your database and than after in your callback you call the closing event programmatically.
I found this post which might help you.
At last i got the answer. My problem was my handlers were not calling and it was because i didn't add Forms Authentication to my app. I added this code after authentication and it worked fine.
FormsAuthentication.RedirectFromLoginPage(UserID, false);
I didn't knew that this was necessary.Thanks every one for helping me..

Issue with jQuery ajax post

This is a followup to this question. Since the problem that I'm now facing is different from that, I thought I'll pose a new question.
I'm having an issue with the following code where in, the request is getting POSTed to the server, but whatever response the server has sent is not visible on the browser (in the form of an alert here).
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#login').delegate('input#submit','click',function(){
var request = $.ajax({
type: "POST",
url: "login",
data: {userid: $("#userid").val(), password:$("#password").val()},
datatype: "xml",
cache: false,
success: function(xml){alert(xml);}
});
});
});
</script>
I can see that this request is going to the server - can see it in the logs. But I don't see the server's response in the browser. Here's the server's response:
<result><url>landing-page</url></result>
Not sure what am I doing wrong, but this seems to be simple stuff that I should get it working without issue. Tried Firebug, but no luck. Any idea as to where am I going wrong here or on how to debug this issue further.
Thanks
Either change your "success" callback to a "complete" callback, or add a complete callback. My guess is that it isn't detecting a "success" even though the response is being successfully retrieved. If it fires the "complete" callback, then you can try and fix your webservice to provide a successful response.

Categories