Mootools ajax cors请求

时间:2016-01-18 10:48:56

标签: ajax cors mootools

我在发件人的脚本上有以下代码:

function sentMessage(array,siteName) {
var myRequest;
var myRequest1;
var domainArray = array; 
var siteName = siteName;

myRequest = new Request({
    url: 'http://127.0.0.1/joomla_3.4.8/administrator/components/com_jnotification/views/javascriptInbox/tmpl/default_body.php',
    method: 'post',
    data: {
            message: <?php echo '"'.strip_tags($message).'"' ; ?>,
            title: <?php echo '"'.$title.'"' ; ?>,
            domainSender: <?php echo '"'.$url.'"' ; ?>,
            domainReceiver: <?php echo '"'.$url1.'"' ; ?>
    },
    onRequest: function(){
        console.log('Loading...');
        //alert('Loading...');
    },
    onProgress: function(event, xhr){
        console.log('Progress ...');
        //alert('Progress...');
    },
    onComplete: function(){
        console.log('Complete...');
        //alert('Complete...');
    },
    onSuccess: function(responseText, responseXML){
       console.log('Success...');

       domainArray.splice(0,1);
       siteName.splice(0,1);

       if (domainArray.length>0){
           sentMessage(domainArray,siteName);
       }

    },
    onFailure: function(xhr){
        console.log('Fail...');
        //alert(xhr.responseText);
        //alert('Fail...');
    },
    onException: function(headerName, value){
        console.log('Exception ...');
        //alert('Exception...');
    }
});
myRequest.send();    
 } 


 default_body.php:

 switch ($_SERVER['HTTP_ORIGIN']) {
 case 'http://127.0.0.1': case 'https://127.0.0.1': case 'http://localhost':    case 'https://localhost':
  header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
  header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  header('Access-Control-Max-Age: 1000');
  header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
  .........................................
 }

我得到以下内容: OPTIONS
 http://127.0.0.1/joomla_3.4.8/administrator/components/com_jnotification/views/javascriptInbox/tmpl/default_body.php c.Request.Class.send @ mootools-core.js:164e.extend。$ owner @ mootools-core.js:38send @ mootools-more.js:248sentMessage @ index.php?option = com_jnotification&amp; view = javascriptOutbox&amp; id = 367:376(匿名函数)@ index.php?option = com_jnotification&amp; view = javascriptOutbox&amp; id = 367:381(匿名函数)@ mootools-core.js:121(匿名函数)@ mootools- core.js:5Array.implement.each @ mootools-core.js:9invoke.fireEvent @ mootools-core.js:120j @ mootools-core.js:175 index.php?option = com_jnotification&amp; view = javascriptOutbox&amp; id = 367:1 XMLHttpRequest无法加载http://127.0.0.1/joomla_3.4.8/administrator/components/com_jnotification/views/javascriptInbox/tmpl/default_body.php。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost”访问。响应的HTTP状态代码为401。

当我使用http://127.0.0.1代替http://localhost时,一切都很完美。 Javascript是Mootools请求方法,窗口的hosts文件127.0.0.1 ip地址指向localhost。

我认为预检请求没有将127.0.0.1解析为localhost,因此该脚本找不到发送请求的正确脚本。

0 个答案:

没有答案
相关问题