本文介绍了MooTools CORS请求与本机Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个MooTools代码:

  new Request.JSON({
method:'POST',
url:URL,/ * URL到另一个域* /
onSuccess:function(r){
callback(r);
}
})post );

此代码不发送POST请求(仅限OPTIONS)...
在下面的代码(它的工作原理):

  var http = null,
params = Object.toQueryString );
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(您的浏览器不支持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



尝试: .setHeader('Content-Type','application / x-www-form-urlencoded');

..在哪里可以有问题?



谢谢!

解决方案

这是因为MooTools会将一些额外的东西包含在请求头中。



例如。如果您的htaccess说:

 标题集Access-Control-Allow-Origin:* 

$>

 c> var foo = new Request({
url:'http://fragged.org/Epitome/example/data/',
method:'get',
onComplete:function data){
//返回一个名字和姓氏的对象
new Element('div [html ={name} {surname}]'substitute(JSON.decode(data)))。 inject(document.body);
}
});

//需要删除,否则CORS需要特别匹配
delete foo.headers ['X-Requested-With'];
foo.send();

这就是为什么你只能看到OPTIONS预飞。它不喜欢你:)



您可以将 .htaccess 更改为 X-Requested-With ,这可能是一些额外的安全性。



请参阅一个工作的例子 - 我刚才那时我想得到这个更改请求已修复。


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 doesn't send POST requests (OPTIONS only)...Look at the code below (it works great):

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 can there be a problem?

Thanks!

解决方案

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.

这篇关于MooTools CORS请求与本机Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:08