2012-05-01 15 views
3

jQuery 1.7.2を使用していて、別のドメインにPOSTリクエストを行いたいとします。これはPOST要求でなければなりません。しかし、これははInternet Explorerで動作しません(IE9で試しました)。他のすべてのブラウザで動作します。Internet ExplorerでのクロスドメインPOSTリクエストajax

私はこのスクリプトを持っている:

<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> 

は、私が戻ってエラーを取得:(IE9を含む)

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

可能複製([クロスオリジンリソース共有を取得する方法(CORS)POSTリクエストが働い] http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin -resource-sharing-cors-post-request-working) – Jon

+0

Relavent:http://stackoverflow.com/a/12014195/545328 – 65Fbef05

+0

duplicate:http://stackoverflow.com/questions/13149122/jquery-ajax-cross- domain-form-submission-issues-with- – inf3rno

答えて

2

Internet Explorerを行います。

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

私のPHPファイルには、次のようになりますCORSをサポートしていません。あなたがプロキシに持っているすべてのクロスドメインリクエスト(カールしてクエリをrepostsし、応答を返し、同じドメイン上のPHPスクリプトに投稿し、)

1

スクリプトは正しく見えるが、私はあなたが変更する必要があると考えている:

{原点} VALUある

header('Access-Control-Allow-Origin: x-requested-with'); 

又は

header('Access-Control-Allow-Origin: {Origin}'); 

から

header('Access-Control-Allow-Origin: *'); 

eを指定します。 Originが与えられていれば '*'を入れるだけではうまくいきません。

また、IE8とIE9のサポートは限られていますが、これまでのようにjQuery.support.cors = trueを入力すると機能します。

+0

私が知る限り、 'jQuery.support.cors'は検出のために読み取り専用です。 – the0ther

+0

@theOtherいいえ、これはjQueryの文書に従って設定できます: "ブラウザがXMLHttpRequestオブジェクトを作成でき、そのXMLHttpRequestオブジェクトにwithCredentialsプロパティがある場合、corsはtrueになります。サポートしていない環境でクロスドメイン要求を有効にするまだクロスドメインXHRリクエスト(Windowsガジェットなど)を許可するには、$ .support.cors = true;を設定します。CORS WD "、http://api.jquery.com/jQuery.support/ – seeg

2

IE < 10でCORSをサポートするには、XDomainRequestオブジェクトを使用するようにajaxメソッドを変更する必要があります。このプラグインはあなたのために行います:https://github.com/jaubourg/ajaxHooks

0

これはIE9で動作します。

<!DOCTYPE html> 
<head> 
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script language="javascript" type="text/javascript"> 
var url = "http://msdn.microsoft.com/en-us/library/windows/desktop/ms759148(v=vs.85).aspx"; 
function getRequest() { 
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } 
    catch (e) {alert("Error while getting 6.0");} 
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } 
    catch (e) {alert("Error while getting 3.0");} 
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } 
    catch (e) {alert("Error while getting 2.0");} 
    throw new Error("This browser does not support XMLHttpRequest."); 
}; 
var request = getRequest(); 
request.open("POST", url, false); 
request.send(); 
alert("Content from :"+url+":"+ request.responseText); 
</script> 
</head> 
<h1>AJAX ActiveX</h1> 
関連する問題