MooTools CORS request vs native Javascript

前端 未结 2 356
清酒与你
清酒与你 2020-12-18 08:41

I have this MooTools code:

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


        
2条回答
  •  攒了一身酷
    2020-12-18 09:08

    This is because MooTools bundles some extra stuff with the request headers.

    eg. if your htaccess says:

    Header set Access-Control-Allow-Origin: *
    

    you need to craft your request like that:

    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();    
    

    This is why you are only seeing the OPTIONS pre-flight. It does not like you :)

    You could change the .htaccess to also match X-Requested-With, which is probably some extra "security".

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

提交回复
热议问题