Cross-subdomain AJAX works in Chrome, not IE - ajax

I have a local build of my site running at local.mydomain.com. I'm making ajax requests to api.mydomain.com which is running on an AWS server and returns JSON. In Chrome, I can call the API no problem. But in IE, I get Access Denied.
After researching, it seems to be a cross-(sub)domain restriction. But I was under the impression that this restriction would apply to both browsers. Can anybody see what might be going wrong here and why it might work in some browsers and not others?

It looks like the problem was in the transport object that IE8+ wants you to use. jQuery uses either ActiveXObject (for IE) or XMLHttpRequest (all others), but IE 8 and above requires XDomainRequest for ajax.
What you can do is return a custom xhr object via $.ajaxSettings.xhr like this,
// override xhr for browser that use XDR
if ('XDomainRequest' in window && window.XDomainRequest !== null) {
// override default jQuery transport
jQuery.ajaxSettings.xhr = function() {
try { return new XDomainRequest(); }
catch(e) {
console.log('test');
}
};
// also, override the support check
jQuery.support.cors = true;
}
I pulled this code from a discussion on the subject here:
http://graphicmaniacs.com/note/getting-a-cross-domain-json-with-jquery-in-internet-explorer-8-and-later/
Definitely take a look at that if you think you're experiencing the same problem.

Related

Cross domain issue on IE

I am working with a project which needs cross domain ajax. I chose cors for this purpose. It works perfectly in chrome and firefox. But in IE, the browser is not creating any ajax call.there is no console errors at all. Can anyone tell me a solution for this?
For IE, you cannot use normal ajax for cross domain access. You need to use XDomainRequest() for this.
Example:
xdr = new XDomainRequest();
if (xdr) {
xdr.onload = function () {
alert(xdr.responseText);
};
xdr.open("get", url);
xdr.send();
}

How can I get http headers in WebBrowser?

I wrote a WP7 web browser app called "Cute Browser".
There is a problem that when navigating to an unkown content-type page such as 'application/stream', the WebBrowser is always navigating.
After my many experiments, I found out a method to detect the always navigating status using invoke the script:
var readyStateChange = function(){
if(document.readyState == 'interactive') {
document.detachEvent('onreadystatechange', readyStateChange);
window.external.Notify('MaybeWml');
}
}
if(document.readyState != 'complete'){
document.attachEvent('onreadystatechange', readyStateChange);
}
I want to know is any possible to get the Http Headers using WebBrowser without start another HttpRequest?

Instanciate Firebreath plugin via JS in IE8

I'm trying to instanciate a plugin made with Firebreath in Internet Explorer 8 but with no luck so far. This is the code that I'm using:
try
{
if(window.ActiveXObject)
{
var plugin = new ActiveXObject("my.plugin");
return true;
}
else
return false;
}
catch(e)
{
return false;
}
This works fine if I put this code in a html page and then open it from disk (also published in my local IIS); but as soon I publish it (it's a facebook application, so, running inside an IFRAME) I get this message (from the catch exception):
Automation server can't create object
I've tried messing around with IE security zones, but nothing happens, always the same message. Is there any workaround for this? I need to be able to get the plugin version (coded inside the plugin).
Thank you!

Find out what exactly XMLHttpRequest is requesting

We're developing a cross-platform application using PhoneGap. At one point, it makes an AJAX request to a relative path, which works fine on both Android and iOS, but not Windows Phone - it gets a 404 response. An absolute URL works fine. The following code:
var a = document.createElement('a');
a.setAttribute('href', 'personalData.html');
console.log(a.href);
also resolves to the correct absolute URL. However, the following:
var xhr = new XMLHttpRequest();
xhr.open("GET", "personalData.html", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr);
}
};
returns 404. I want to find out what's going on. Is there any way to know what absolute path XHR is requesting?
XMLHttpRequest is a JavaScript object that was designed by Microsoft and adopted by Mozilla, Apple, and Google, it's not related to Phonegap.
https://developer.mozilla.org/en/DOM/XMLHttpRequest
Said this, you could try using an http Proxy like Fiddler to view all http trafic.
http://www.fiddler2.com/fiddler2/
Best regards.
In these cases, Fiddler Web Debugger is unbeatable. It will tell you exactly what the request is doing.
It also works with the Windows Phone emulator. To debug an actual device, setup FIddler to accept external connections and assign Fiddler as a proxy on the phone.
I have done both scenarios, works fine.
Give it a shot.
I have try your code in my project (Phonegap/WinPhone7) and your code didn't get anything till I initialized the request (xhr.send();).
I have no idea how you make request without this method.

FBJS AJAX.post is not working after permissions dialog

I have problems with facebook application based on flash which communicate with PHP using FBJS-bridge. When someone use the application for the first time, he/she is asked for various permissions. After that, flash contact PHP with ajax but request is never sent. When you refresh page, everything is working without any problems.
If you remove the application on privacy settings, refresh the page and try again - same bug happens. If you allow application, refresh page, in other tab remove application and start application in previous tab - user is asked for permissions but everything is working after allowing application.
This is FBJS code
function openPermissions(){
Facebook.showPermissionDialog(/*permissions string*/, permissionOnDone);
}
function permissionOnDone(perms){
if (!perms) {
document.getElementById("indexswf").callSWF('noallow');
} else {
document.getElementById("indexswf").callSWF('allow');
}
}
function ajaxCall(url,parameters){
var params = {};
for(var i=0;i<parameters.length;i+=2){
params[parameters[i]]=parameters[i+1];
}
ajax = new Ajax();
ajax.requireLogin = true;
ajax.responseType = Ajax.RAW;
ajax.ondone = function(data){
document.getElementById("indexswf").callSWF('parseAjax', data);
}
ajax.post('http://the.url.to/the_real_server/not_to_the_fb_url/'+url,params);
}
openPermissions is called to display permission dialog, and on allow flash function allow() is called. In flash, allow() calls JS function ajaxCall(), which should make ajax request. But, ajax.post never sends request. I know that for sure, because flash function parseAjax was never called and also debugging tools in browsers are not showing any ajax requests. URL and parameters are same as when it is working. No flash or JS errors are detected...
Anyone have idea what is wrong here? Maybe facebook bug again since this was all working few days ago...
ajax.requireLogin = true should be set to false for some reason

Resources