文章出處

今天在開發的時候,遇到一個問題,$.get()在 IE8 瀏覽器不起作用,但 Chrome,Firefox 卻是可以的,網上資料很多,最后發現是 IE8 默認不支持 CORS 請求,需要手動開啟下:

jQuery.support.cors = true;

//url 是跨域的地址
$.get(url, , function (data) {
    //...
});

參考資料:Ajax call not working in IE8


后來發現上面的設置在 IE8/IE9 中無效,需要另外的解決方案:jQuery-ajaxTransport-XDomainRequest

示例寫法:

$.getJSON('http://jsonmoon.jsapp.us/').done(function(data) {
  console.log(data.name.first);
});

// POST
$.ajax({
  url: 'http://frozen-woodland-5503.herokuapp.com/cors.json',
  data: 'this is data being posted to the server',
  contentType: 'text/plain',
  type: 'POST',
  dataType: 'json'
}).done(function(data) {
  console.log(data.name.last);
});

參考資料:IE8、9 下的資源跨域請求


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()