在Internet Explorer中的跨域POST请求阿贾克斯 [英] Cross-domain POST request ajax in internet explorer

查看:333
本文介绍了在Internet Explorer中的跨域POST请求阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery 1.7.2,并希望做一个POST请求到另一个域。它必须是一个POST请求。但这在Internet Explorer中无法正常工作(我试过IE9);它适用于所有其他浏览器。

我有这样的脚本:

<脚本> jQuery.support.cors = TRUE; jQuery的(函数(){     $阿贾克斯({         跨域:真正的,         缓存:假的,         键入:POST,         网址:HTTP://someotherdomain/test.php,         数据: {},         成功:函数(DA){             的console.log(JSON.stringify(DA))         },         错误:函数(jqxhr){             的console.log('故障')             的console.log(JSON.stringify(jqxhr))         },         数据类型:JSON     }); }); < / SCRIPT>

我找回了错误:

{readyState的0状态:0,状态文本:错误:拒绝访问\ r。 \ N}

我的PHP文件看起来是这样的:

< PHP 标题(访问控制 - 允许 - 产地:*'); 标题(访问控制 - 允许 - 方法:GET,POST,DELETE,PUT,OPTIONS'); 回声json_de code(阵列('成功'=>'是'));

解决方案

要支持IE&LT CORS; 10,您必须修改AJAX方法使用XDomainRequest对象。这个插件会为你: https://github.com/jaubourg/ajaxHooks

I'm using jQuery 1.7.2 and would like to make a POST request to another domain. It must be a POST request. But this does not work in internet explorer (I tried on IE9); it works on all other browsers.

I have this script:

<script>
jQuery.support.cors = true;

jQuery(function() {
    $.ajax({
        crossDomain : true,
        cache: false,
        type: 'POST',
        url: 'http://someotherdomain/test.php',
        data: {},
        success: function(da) {
            console.log(JSON.stringify(da))
        },
        error: function(jqxhr) {
            console.log('fail') 
            console.log(JSON.stringify(jqxhr))
        },
        dataType: 'json'
    });
});
</script>

I get back the error:

{"readyState":0,"status":0,"statusText":"Error: Access denied.\r\n"}

My PHP file looks like this:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS');
echo json_decode(array('success' => 'yes'));

解决方案

To support CORS in IE < 10, you must modify the ajax method to use the XDomainRequest object. This plugin does it for you: https://github.com/jaubourg/ajaxHooks

这篇关于在Internet Explorer中的跨域POST请求阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆