PDA

View Full Version : extjs4 - Ext.Ajax.Request and Cross domain



tdikarim
2 Jul 2013, 1:38 AM
Hi all,

Does anyone have a sample for process a request ajax on cross domain ?

I usually use this, but it does not work with cross domain :


Ext.Ajax.request({
url: DIR_SERVEUR_PHP + 'eCall_Ajax.php',
method: "GET",
params: {
"Type": 'REPRINT',
"User": 'user',
"Action": 'REPRINT',
"listeCmd" : json_selRows,
"docToPrint" : data.codeDoc
},
scope: this,
success: function (result) {
var response = Ext.decode(result.responseText);
if (response.success == true) {
reloadAllGrids();
}
else{
//--- Une erreur s'est produite
alert(response.returnError);
}
},
failure: function (result) {
alert('REPRINT (' + data.codeDoc + ') > something failed');
}
});


Thanks for your suggestions

julio.batista
2 Jul 2013, 2:53 AM
JSONP is a good alternative

tvanzoelen
2 Jul 2013, 3:22 AM
JSONP is for proxies. You could try the cors flag. But that is supported from IE8+

skirtle
2 Jul 2013, 5:03 AM
JSONP is for proxies. You could try the cors flag. But that is supported from IE8+

ExtJS supports JSON-P just like AJAX, via Ext.data.JsonP:

http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.JsonP

CORS is usually preferable if you can accept the browser restrictions. I strongly recommend researching CORS (cross-origin resource sharing) before trying to use it, it isn't magic.

To turn on CORS for all your requests you can do something like this:


Ext.Ajax.cors = true;
Ext.Ajax.useDefaultXhrHeader = false;

That second option prevents ExtJS adding in a custom header, which would force CORS into a pre-flight OPTIONS request. If you just want CORS for a single request then move that first option into the request call.

Whether you choose to use JSON-P or CORS you'll need to have support on the server. There is no way to make a cross-origin request (and view the response) if the server isn't written to handle it.

tvanzoelen
2 Jul 2013, 5:35 AM
ExtJS supports JSON-P just like AJAX, via Ext.data.JsonP:

Ah good to know! I did not know that class had a request function.

tdikarim
2 Jul 2013, 6:21 AM
Hi,

Thanks to you all.

It's work fine now by adding this :
on server side :


<?php header("Access-Control-Allow-Origin: xxxxx"); ?>
<?php header("Access-Control-Allow-Methods: POST, GET, OPTIONS"); ?>


on client side :


Ext.Ajax.useDefaultXhrHeader = false;

mojtabak
14 Oct 2014, 2:51 PM
Hi There,

I am using

Ext.Ajax.useDefaultXhrHeader = false;
Ext.Ajax.cors = true;

for sending CORS POST request. This seems only works with Ext 4 and not Ext 5.

Your help would be appriciated.

Regards,

M.