我已经在阳光下尝试了所有东西,但却无法正常工作。
尝试在IE 8 + 9中获得一个简单的GET请求交叉域..在Chrome和Firefox以及IE10中运行良好。尝试使用XDomainRequest但没有骰子..得到未定义的错误。
function RequestWrapper (url, data, error, success, method) {
// IE8 & 9 only Cross domain JSON GET request
if ('XDomainRequest' in window && window.XDomainRequest !== null) {
var xdr = new XDomainRequest(); // Use Microsoft XDR
xdr.open(method, url);
xdr.onload = function () {
var dom = new ActiveXObject('Microsoft.XMLDOM'),
JSON = $.parseJSON(xdr.responseText);
dom.async = false; // I've tried both true and false
if (JSON == null || typeof (JSON) == 'undefined') {
JSON = $.parseJSON(data.firstChild.textContent);
}
success(JSON);
};
xdr.onerror = function () {
error();
}
xdr.send();
}
// Do normal jQuery AJAX for everything else
else {
$.ajax({
url: url,
type: method,
data: data,
dataType: 'json',
success: success,
error: error,
xhrFields: { withCredentials: true }
});
}
}
RequestWrapper(
// URL
'http://myURL',
// Data
null,
// error
function (xhr, status, error) { console.log('error: ' + status); },
// success
function (data) { console.log('success: ' + data); },
// method
'get'
);
编辑: 我尝试使用jsonp但得到一个parsererror。还尝试了iecors.js jQuery ajax自定义传输(https://github.com/dkastner/jquery.iecors)..仍然没有骰子
<script src="jquery.iecors.js"></script>
<script type="text/javascript">
function RequestWrapper (url, data, error, success, method) {
$.ajax({
url: url,
type: method,
data: data,
dataType: 'jsonp',
success: success,
error: error,
xhrFields: { withCredentials: true }
});
}
RequestWrapper(
// URL
'http://givingimages.pixfizz.com/v1/users/1891548/books.json',
// Data
null,
// error
function (xhr, status, error) { console.log('error: ' + status); },
// success
function (data) { console.log('success: ' + data); },
// method
'get'
);
</script>
答案 0 :(得分:0)
尝试下面的代码这是基本的例子,它的工作正常我已经测试了所有浏览器。
<html>
<head>
<!-- Include jquery from Google here -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
// Wait till dom is loaded
$(document).ready(function() {
// Load ajax.php as JSON and assign to the data variable
$.getJSON('ajax.php', function(data) {
// set the html content of the id myThing to the value contained in data
$("#myThing").html(data.value);
});
});
</script>
</head>
<body>
<p id="myThing"></p>
</body>
</html>
创建ajax.php文件
<?php
echo json_encode(array("value" => "Hello World"));
?>
我认为这篇文章对您有用click here to view script