MooTools CORS request versus native Javascript

I have this MooTools code:

new Request.JSON({ method: 'POST', url: URL, /*URL TO ANOTHER DOMAIN*/ onSuccess: function(r){ callback(r); } }).post(data); 

And this code does not send POST requests (OPTIONS only) ... Look at the code below (it works fine):

 var http = null, params = Object.toQueryString(data); try { http = new XMLHttpRequest(); } catch (e) { try { http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { http = null; alert("Your browser does not support AJAX!"); } } } var url = URL; http.onreadystatechange = function () { if (http.readyState == 4 && http.status == 200) { var jsonData = JSON.parse(http.responseText); /*OR EVAL*/ callback(jsonData); } }; http.open("POST", url); http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); http.send(params); 

EDIT

Tried: .setHeader('Content-Type','application/x-www-form-urlencoded');
Still nothing ... Where could the problem be?

Thanks!

+4
source share
2 answers

This is because MooTools associates some additional files with request headers.

eg. if your htaccess says:

 Header set Access-Control-Allow-Origin: * 

you need to write your request as follows:

 var foo = new Request({ url: 'http://fragged.org/Epitome/example/data/', method: 'get', onComplete: function (data) { // returns an object with name and surname new Element('div[html="{name} {surname}"]'.substitute(JSON.decode(data))).inject(document.body); } }); // need to remove that or CORS will need to match it specifically delete foo.headers['X-Requested-With']; foo.send(); 

That is why you see only the preliminary OPTIONS instructions. You don't like it :)

You can modify .htaccess in the same way as X-Requested-With , which is probably some additional "security".

See http://jsfiddle.net/7zUSu/1/ for a working example. I did this a while ago when I wanted to get this change in the Request https://github.com/mootools/mootools-core/issues/2381 fixed.

+8
source

What do you mean by (OPTIONS only)? Both examples send a POST request, the only difference is in the Accept request headers.

MooTools sends Accept: application/json , and native sends Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 .

This may affect server response.

0
source

Source: https://habr.com/ru/post/1498964/


All Articles